matching types in scala -


Is it possible to match types in Scala? Something like this: def [T] = T match {case String = & gt; "You gave me a string", Case Array = & gt; "You gave me an array" case _ = & gt; "I do not know how it is!" }

(but it's compiled, obviously :))

Or maybe the right way is overloaded ?? Is this possible?

Unfortunately, I can not give an example of an object and pattern match on it.

You can use the manifest and match a pattern on them. When the problem of passing an array class is problematic, because as JVM uses a different class for each array type. To work around this problem, you can check whether a class in question is erased in the array or not: string {string} def [T: Manifest] = manifest [T] match {case stringmanfast = & gt; "You gave me the string" case x if x.erasure.isArray = & gt; "You gave me an array" case _ = & gt; "I do not know how it is!" }

Comments