{"action":"created","issue":{"url":"https://api.github.com/repos/pocorall/scaloid/issues/118","labels_url":"https://api.github.com/repos/pocorall/scaloid/issues/118/labels{/name}","comments_url":"https://api.github.com/repos/pocorall/scaloid/issues/118/comments","events_url":"https://api.github.com/repos/pocorall/scaloid/issues/118/events","html_url":"https://github.com/pocorall/scaloid/issues/118","id":58381762,"number":118,"title":"org.scaloid.common.runOnUiThread","user":{"login":"PkmX","id":610615,"avatar_url":"https://avatars.githubusercontent.com/u/610615?v=3","gravatar_id":"","url":"https://api.github.com/users/PkmX","html_url":"https://github.com/PkmX","followers_url":"https://api.github.com/users/PkmX/followers","following_url":"https://api.github.com/users/PkmX/following{/other_user}","gists_url":"https://api.github.com/users/PkmX/gists{/gist_id}","starred_url":"https://api.github.com/users/PkmX/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/PkmX/subscriptions","organizations_url":"https://api.github.com/users/PkmX/orgs","repos_url":"https://api.github.com/users/PkmX/repos","events_url":"https://api.github.com/users/PkmX/events{/privacy}","received_events_url":"https://api.github.com/users/PkmX/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":3,"created_at":"2015-02-20T17:10:26Z","updated_at":"2015-02-20T18:27:22Z","closed_at":null,"body":"`org.scaloid.common.runOnUiThread` currently has the signature:\r\n\r\n```scala\r\ndef runOnUiThread[T >: Null](f: ⇒ T): T\r\n```\r\n\r\nIf it is called with a block that returns `Unit`, and since `Unit <: AnyVal` and `Null <: AnyRef`, Scala can only infer `T` to be `Any`, which gives an annoying warning message that `Any` is inferred if compiling with `-Xlint` enabled.\r\n\r\nThe real issue here however is that I believe *returning the result of `f` or `null` depending on whether `runOnUiThread` is called on the UI thread* is a misdesign. The caller either calls this function from a non-UI thread to perform side effects on the UI thread, and in this case `f` should return `Unit`. If the caller is already on the UI thread, he can just call `f` directly to collect the result. Also, returning `null` is basically asking for `NullPointerException` if the user is not careful about this.\r\n\r\nTherefore, my proposal is to change the signature to the simpler:\r\n\r\n```scala\r\ndef runOnUiThread(f: => Unit): Unit\r\n```\r\n\r\nand remove the return statements. I can send a pull request if you are okay with this change."},"comment":{"url":"https://api.github.com/repos/pocorall/scaloid/issues/comments/75292874","html_url":"https://github.com/pocorall/scaloid/issues/118#issuecomment-75292874","issue_url":"https://api.github.com/repos/pocorall/scaloid/issues/118","id":75292874,"user":{"login":"PkmX","id":610615,"avatar_url":"https://avatars.githubusercontent.com/u/610615?v=3","gravatar_id":"","url":"https://api.github.com/users/PkmX","html_url":"https://github.com/PkmX","followers_url":"https://api.github.com/users/PkmX/followers","following_url":"https://api.github.com/users/PkmX/following{/other_user}","gists_url":"https://api.github.com/users/PkmX/gists{/gist_id}","starred_url":"https://api.github.com/users/PkmX/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/PkmX/subscriptions","organizations_url":"https://api.github.com/users/PkmX/orgs","repos_url":"https://api.github.com/users/PkmX/repos","events_url":"https://api.github.com/users/PkmX/events{/privacy}","received_events_url":"https://api.github.com/users/PkmX/received_events","type":"User","site_admin":false},"created_at":"2015-02-20T18:27:22Z","updated_at":"2015-02-20T18:27:22Z","body":"> Come to think of it, simply refitting this to return a `Future` might not be such a bad idea; truly asynchronous callers can just discard it upon the return of `runOnUiThread()`.\r\n\r\nI think it's better to overload it (untested):\r\n\r\n```scala\r\n  def runOnUiThread(f: => Unit): Unit = {\r\n    if (uiThread == Thread.currentThread) {\r\n      f\r\n    } else {\r\n      handler.post(new Runnable() {\r\n        def run(): Unit = f\r\n      })\r\n    }\r\n  }\r\n\r\n  def runOnUiThread[T](f: => T): Future[T] = {\r\n    val p = Promise[T]()\r\n    if (uiThread == Thread.currentThread) {\r\n      p.complete(Try(f))\r\n    } else {\r\n      runOnUiThread {\r\n        p.complete(Try(f))\r\n      }\r\n    }\r\n    p.future\r\n  }\r\n```"}}