scala - Can we use match to check the type of a class -


i'm new scala, , i'm learning match keyword now.

i wanna know if can use keyword match check type of class. code is:

object main {     def main(args: array[string]) {         val x = "aa"         checktype(x)     }      def checktype(cls: anyref) {         cls match {             case string => println("is string")             case date => println("is date")             case _ => println("others")         }     } } 

the code can't compiled, so, it's impossible this? scala-way check type of class? it:

if(cls.isinstanceof[string]) { ... } else if(cls.isinstanceof[date]) { ... } else { ... } 

right?

this will compile:

def checktype(cls: anyref) {                       cls match {                                      case s: string => println("is string")     case d: date => println("is date")         case _ => println("others")                }                                                    } 

... or simplified version of that:

def checktype(cls: anyref) =   cls match {                                      case _: string => println("is string")     case _: date => println("is date")         case _ => println("others")                }                                                    

Comments

Popular posts from this blog

c# - How to set Z index when using WPF DrawingContext? -

razor - Is this a bug in WebMatrix PageData? -

visual c++ - Using relative values in array sorting ( asm ) -