I can't quite understand why implicit conversions from Scala to JS values don't work this way:
import scala.scalajs.js
import scala.scalajs.js.JSConverters._
trait X extends js.Object {
def map(f: js.Function1[Int, String]) = js.native
def mapScala(f: Function1[Int, String]) = js.native // just for demo purpose, this is not really part of type X
def create(maybe: js.UndefOr[Int]) = js.native
}
def test(x: X) {
// I want these two lines to compile, but they don't
x.map(a => "yo") // error: missing parameter type
x.create(None) // error: type mismatch. Found None.type, required: js.UndefOr[Int]
// This works, but is too verbose
x.map((a: Int) => "yo") // only if arg type specified like this
x.mapScala(a => "yo") // because mapScala expects a Scala Function1
x.create(JSRichOption(None).orUndefined) // because value is manually converted
}
Am I trying to use implicit conversions the wrong way? The implementation of class X
is provided externally, in Javascript. In my code I want to pass native Scala values to methods of X
, I don't want to do the conversion manually each time.
I know there's an alternative way for Scala.js – to pimp my type X
like this, but I'm trying to avoid that because it's more boilerplate and a lot more objects will be instantiated at runtime that way. Regardless of that, I still do want to understand why exactly my code isn't working.