Created
May 10, 2013 17:40
-
-
Save coderunner/5556058 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| object scrap { | |
| trait Value | |
| case class StringValue(value: String) extends Value | |
| case class LongValue(value: Long) extends Value | |
| val value: Value = StringValue("test") | |
| /**** Matching ****/ | |
| val StringValue(s1) = value | |
| //s1: String = test | |
| val LongValue(l1) = value | |
| /* | |
| scala.MatchError: StringValue(test) (of class StringValue) | |
| at .<init>(<console>:13) | |
| at .<clinit>(<console>) | |
| at .<init>(<console>:11) | |
| at .<clinit>(<console>) | |
| at $print(<console>) | |
| at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) | |
| at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57 | |
| ) | |
| at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl | |
| .java:43) | |
| at java.lang.reflect.Method.invoke(Method.java:601) | |
| at scala.tools.nsc.interpreter.IMain$ReadEvalPrint.call(IMain.scala:704) | |
| at scala.tools.nsc.interpreter.IMain$Request$$anonfun$14.apply(IMain.scala:920) | |
| at scala.tools.nsc.interpreter.Line$$anonfun$1.apply$mcV$sp(Line.scala:43) | |
| at scala.tools.nsc.io.package$$anon$2.run(package.scala:25) | |
| at java.lang.Thread.run(Thread.java:722) | |
| */ | |
| /**** Instanceof ****/ | |
| val s2 = value.asInstanceOf[StringValue].value | |
| //s2: String = test | |
| val l2 = value.asInstanceOf[LongValue].value | |
| /* | |
| java.lang.ClassCastException: StringValue cannot be cast to LongValue | |
| at .<init>(<console>:13) | |
| at .<clinit>(<console>) | |
| at .<init>(<console>:11) | |
| at .<clinit>(<console>) | |
| at $print(<console>) | |
| at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) | |
| at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57 | |
| ) | |
| at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl | |
| .java:43) | |
| at java.lang.reflect.Method.invoke(Method.java:601) | |
| at scala.tools.nsc.interpreter.IMain$ReadEvalPrint.call(IMain.scala:704) | |
| at scala.tools.nsc.interpreter.IMain$Request$$anonfun$14.apply(IMain.scala:920) | |
| at scala.tools.nsc.interpreter.Line$$anonfun$1.apply$mcV$sp(Line.scala:43) | |
| at scala.tools.nsc.io.package$$anon$2.run(package.scala:25) | |
| at java.lang.Thread.run(Thread.java:722) | |
| */ | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment