Search code examples
scalascala.jsscala.rx

Overloaded method value trigger with alternatives for '=> Unit' parameter


I just upgraded Scala.rx version 0.3.2 to 0.4.0 and suddenly I get the following errors on my triggers:

overloaded method value trigger with alternatives:
[error]   (f: Boolean => Unit)(implicit ownerCtx: rx.Ctx.Owner)rx.Obs <and>
[error]   (thunk: => Unit)(implicit ownerCtx: rx.Ctx.Owner)rx.Obs
[error]   cannot be applied to (() => org.scalajs.jquery.JQuery)
[error]   MyRx.trigger { () =>

For all my method calls looking like this:

import rx.Ctx.Owner.Unsafe._

val MyRx: Var[Boolean] = Var[Boolean](false)

MyRx.trigger {
   // ...
   jQuery("#page_content").css("opacity", 1)
}

Somehow I am unable to resolve this issue. How do I solve this?

This seems to work:

MyRx.trigger {
  // ...
  jQuery("#page_content").css("opacity", 1)
  ()
}

But I definetly would prefer a solution like this:

MyRx.trigger {
  // ...
  jQuery("#page_content").css("opacity", 1)
}: => Unit // doesnt compile

Solution

  • Note MyRx.trigger { () => in the error message. First, you need to remove the () => part (it may be on the next line after { as well).

    => Unit as a parameter type is a by-name parameter, it automatically turns a block like { ...; jQuery("#page_content").css("opacity", 1) } into { () => ...; jQuery("#page_content").css("opacity", 1) }.

    In this case, discarding doesn't seem to happen, you can explicitly discard the result e.g. as

    val _ = jQuery("#page_content").css("opacity", 1)
    

    or

    jQuery("#page_content").css("opacity", 1): Unit
    

    If you want to put it on the entire block, the syntax will be uglier:

    MyRx.trigger({
       // ...
       jQuery("#page_content").css("opacity", 1)
    }: Unit)