Created
November 5, 2014 17:17
-
-
Save soc/7fbfbf0390af1f18d410 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
| case class Person(firstName: String, lastName: String, age: Int) | |
| object Person extends App { | |
| // Convert Strings like "Hans Maier, 23" into a Person instance. | |
| def apply(value: String): Person = { | |
| import java.util.regex._ | |
| val pattern = Pattern.compile("""(\w+) (\w+), (\d+)""") | |
| val matcher = pattern.matcher(value) | |
| new Person(matcher.group(1), matcher.group(2), matcher.group(3).toInt) | |
| } | |
| println(Person("Hans Maier, 23")) | |
| } | |
| /* | |
| Exception in thread "main" java.lang.IllegalStateException: No match found | |
| at java.util.regex.Matcher.group(Matcher.java:536) | |
| at workshop.Person$.apply(Person.scala:16) | |
| at workshop.Person$.delayedEndpoint$workshop$Person$1(Person.scala:19) | |
| at workshop.Person$delayedInit$body.apply(Person.scala:10) | |
| at scala.Function0$class.apply$mcV$sp(Function0.scala:40) | |
| 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 workshop.Person$.main(Person.scala:10) | |
| at workshop.Person.main(Person.scala) | |
| */ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment