I would like to call, from ScalaJS, a JS function similar to the following:
function parseExpression(s){
if(isNumber(s)) return {lit:s}
else{
return {
left : leftOp(s)
oper : operand(s)
right: rightOp(s)
}
}
}
where the returned object can be of two different shapes, either {lit}
or {left, oper, right}
I have defined the following traits in ScalaJS:
@js.native
trait NumLiteral extends js.Object{
val lit:String = js.native
}
@js.native
trait NumOper extends js.Object{
val left : NumLiteral | NumOper = js.native
val oper : String = js.native
val right: NumLiteral | NumOper = js.native
}
then I can declare function parseExpression
as Function1[String, NumLiteral | NumOper]
What is the best way of checking if the returned value of parseExpression(someExpressionString)
is of type NumLiteral
or NumOper
? I am free to change the JS and/or the Scala code to achieve the most elegant solution.