File tree Expand file tree Collapse file tree 1 file changed +10
-4
lines changed
Expand file tree Collapse file tree 1 file changed +10
-4
lines changed Original file line number Diff line number Diff line change @@ -23,8 +23,8 @@ object CustomerID {
2323 def apply(name: String) = s"$name--${Random.nextLong}"
2424
2525 def unapply(customerID: String): Option[String] = {
26- val name = customerID.split("--").head
27- if (name. nonEmpty) Some(name ) else None
26+ val stringArray: Array[String] = customerID.split("--")
27+ if (stringArray.tail. nonEmpty) Some(stringArray.head ) else None
2828 }
2929}
3030
@@ -45,12 +45,18 @@ val CustomerID(name) = customer2ID
4545println(name) // prints Nico
4646```
4747
48- This is equivalent to ` val name = CustomerID.unapply(customer2ID).get ` . If there is no match, a ` scala.MatchError ` is thrown:
48+ This is equivalent to ` val name = CustomerID.unapply(customer2ID).get ` .
4949
50- ``` tut:fail
50+ ``` tut
5151val CustomerID(name2) = "--asdfasdfasdf"
5252```
5353
54+ If there is no match, a ` scala.MatchError ` is thrown:
55+
56+ ``` tut:fail
57+ val CustomerID(name3) = "-asdfasdfasdf"
58+ ```
59+
5460The return type of an ` unapply ` should be chosen as follows:
5561
5662* If it is just a test, return a ` Boolean ` . For instance ` case even() ` .
You can’t perform that action at this time.
0 commit comments