Passing Scala Class as parameter?

前端 未结 3 564
故里飘歌
故里飘歌 2021-02-01 19:45

I\'m looking to pass a Class as a parameter to a Scala function like so:

def sampleFunc (c : Class) : List[Any]  

(Side question: should the ty

3条回答
  •  盖世英雄少女心
    2021-02-01 20:01

    In Scala, if you need to deal with classes, there is probably a different way to approach the problem.

    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 should work

    def checkType(obj: Any) = obj match {
      case y: Int => "get int"
      case _ => "Other"
    }
    

    See http://docs.scala-lang.org/tutorials/tour/pattern-matching.html

提交回复
热议问题