Reputation: 2221
I've defined function in Scala.js like this:
val myFunction: js.Function1[js.Array[String], Boolean] = (data: js.Array[String]) => true
And also defined Javascript function on the page:
function callFunction(inputFunction){
console.log(inputFunction(["10"]))
}
When I make a call from Scala.js:
GlobalScope.callFunction(myFunction)
I got error:
Uncaught TypeError: inputFunction is not a function
What I'm doing wrong and how to fix it?
Definition of the GlobalScope
:
@JSGlobalScope
@js.native
object GlobalScope extends js.Any {
def GlobalScope.callFunction(someFunction: (js.Array[String]) => Boolean): Unit = js.native
}
Upvotes: 1
Views: 395
Reputation: 22105
The definition of callFunction
in GlobalScope
is erroneous. It asks for a js.Array[String] => Boolean
, which is a Scala function from js.Array[String]
to Boolean
, not a JavaScript function. When you call it, even if you give it a js.Function1[js.Array[String], Boolean]
(which is correct), the Scala type system will insert an implicit conversion from js.Function1
to scala.Function1
to conform to the expected type. But then of course the JavaScript code in callFunction
receives a Scala function and is not happy with it.
The solution is to fix the definition of callFunction
to take a js.Function1
:
@JSGlobalScope
@js.native
object GlobalScope extends js.Any {
def callFunction(someFunction: js.Function1[js.Array[String], Boolean]): Unit = js.native
}
Upvotes: 1