将 Scala 类作为参数传递?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28112891/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Passing Scala Class as parameter?
提问by GroomedGorilla
I'm looking to pass a Class as a parameter to a Scala function like so:
我希望将 Class 作为参数传递给 Scala 函数,如下所示:
def sampleFunc (c : Class) : List[Any]
(Side question: should the type in the parameter be Class or Class[_]?)
(附带问题:参数中的类型应该是 Class 还是 Class[_]?)
The reason I'm passing a Class type is to check whether an object belongs to a particular type or not. At the moment, as a workaround, I'm passing a sample object to the method and comparing the two object's .getClass result. I hardly think this is ideal though and I'm sure there's a clearly defined way of passing Types in Scala.
我传递 Class 类型的原因是检查对象是否属于特定类型。目前,作为一种解决方法,我将一个示例对象传递给该方法并比较两个对象的 .getClass 结果。我几乎不认为这是理想的,而且我确信在 Scala 中有一种明确定义的传递类型的方法。
回答by Herrington Darkholme
Well, to your original question: Yes, you can pass Scala Class as an argument.
As Classis a type constructor, it needs a concrete type to make a type to be used in argument. You can use the wildcard existential type Class[_]as you have done.
好吧,对于您最初的问题:是的,您可以将 Scala 类作为参数传递。与Class类型构造函数一样,它需要一个具体的类型来创建要在参数中使用的类型。您可以Class[_]像以前一样使用通配符存在类型。
def sample(c: Class[_]) = println("Get a class")
sample(classOf[Int])
However, if you want to check whether an object is of certain type, I recommend you to use =:=, in combination with default parameter
但是,如果您想检查对象是否属于某种类型,我建议您使用=:=, 结合默认参数
def checkType[T](obj: T)(implict ev: T =:= Int = null) =
if (ev == null) "get other"
else "get int"
checkType(123) // get int
checkType("123") // get other
回答by dk14
Yes. In general case it's ClassTag/TypeTag- see Mirrorsand Reflection
是的。在一般情况下它是ClassTag/ TypeTag- 见镜子和反射
import scala.reflect._
def runtimeClass[T: ClassTag] = classTag[T].runtimeClass
scala> runtimeClass[String]
res2: Class[_] = class java.lang.String
If you really need only to check compile-time (formal) type equality
如果您真的只需要检查编译时(正式)类型相等性
scala> implicitly[String =:= Double]
<console>:14: error: Cannot prove that String =:= Double.
implicitly[String =:= Double]
^
If you really need to check only type of object:
如果你真的只需要检查对象的类型:
scala> "aa".isInstanceOf[String]
res4: Boolean = true
回答by Gonfva
In Scala, if you need to deal with classes, there is probably a different way to approach the problem.
在 Scala 中,如果您需要处理类,可能有不同的方法来解决这个问题。
So I'm not claiming to resolve the title of the question, but the specific need that you post. To check if a specific object is of a specific type, the typical approach in Scala is the use of pattern matching. Unless I'm missing something, this shouldwork
所以我并不是要解决问题的标题,而是要解决您发布的特定需求。要检查特定对象是否属于特定类型,Scala 中的典型方法是使用模式匹配。除非我遗漏了什么,否则这应该有效
def checkType(obj: Any) = obj match {
case y: Int => "get int"
case _ => "Other"
}
See http://docs.scala-lang.org/tutorials/tour/pattern-matching.html
请参阅http://docs.scala-lang.org/tutorials/tour/pattern-matching.html

