1

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
}
2
  • What is the definition of GlobalScope and its method callFunction? Commented Jul 14, 2018 at 9:33
  • @sjrd I've added definition of GlobalScope to my question Commented Jul 14, 2018 at 11:44

1 Answer 1

1

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
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.