有没有办法确定Scala.JS中的类型是Scala还是JS?

时间:2015-07-19 15:56:20

标签: scala.js

在Scala JS中有一种方法可以确定某个值是Scala还是本机js?

object Bar
case class Foo(x: Int)

def isAScalaType(x: Any) : Boolean = {/* What is the implementation of this function */}

isAScalaType(js.Dynamic.literal(Hello="World")) // returns false

isAScalaType(Foo(1)) // returns true
isAScalaType(Bar) // returns true

//Wrappers such as Int / Double / String etc should also return true:
isAScalaType("abc") // true
isAScalaType(123) // true
isAScalaType(1.0) // true

(这个问题最初是在gitter聊天室中提出的)

1 个答案:

答案 0 :(得分:2)

建议的方法是在值上调用.getClass并查看它是否为null

e.g。

def isAScalaType(x: Any) : Boolean = x.getClass != null

相关问题