Hi,
I am struggling to understand why the two snippets below will behave differently or 'null' is the problem here?. One executes successfully and other throws the runtime scala.MatchError.
Snippet 1:
val str = "HELP"
val perfectTuple: (String, String) = str match {
case "NO-HELP" => ("First Help", "Second Help")
case "OTHER-HELP" => ("I won't Help!", "Even,I won't Help!")
case "HELP" => (null,"Single Help")
case _ => throw new NoSuchMethodException
}
==================
Snippet 2:
val str = "HELP"
val (firstPart:String, secondPart:String) = str match {
case "NO-HELP" => ("First Help", "Second Help")
case "OTHER-HELP" => ("I won't Help!", "Even,I won't Help!")
case "HELP" => (null,"Single Help")
case _ => throw new NoSuchMethodException
}
======================
As we can see that there is a little difference between the two snippets , one storing the result in a tuple 2 value and the other is extracting the value out of tuple 2 and storing them in two values. Snippet1 executes successfully but snippet 2 throws the runtime error , below:
Exception in thread "main" scala.MatchError: (null,Single Help) (of class scala.Tuple2)
at com.trn.scala.problems.P05$.delayedEndpoint$com$trn$scala$problems$P05$1(P05.scala:10)
at com.trn.scala.problems.P05$delayedInit$body.apply(P05.scala:6)
at scala.Function0$class.apply$mcV$sp(Function0.scala:34)
at scala.runtime.AbstractFunction0.apply$mcV$sp(AbstractFunction0.scala:12)
at scala.App$$anonfun$main$1.apply(App.scala:76)
at scala.App$$anonfun$main$1.apply(App.scala:76)
at scala.collection.immutable.List.foreach(List.scala:381)
at scala.collection.generic.TraversableForwarder$class.foreach(TraversableForwarder.scala:35)
at scala.App$class.main(App.scala:76)
at com.trn.scala.problems.P05$.main(P05.scala:6)
at com.trn.scala.problems.P05.main(P05.scala)
I have executed this on scala 2.10.5 as well as 2.11.7 .P05 is my object name above.
Please advise if i am missing the obvious here?
Thanks
Tarun