July 2017
Intermediate to advanced
284 pages
6h 45m
English
If you want to perform more powerful extractions and apply a series of pattern matching, you can use Scala extractors. In the next example, you’ll write an extractor to parse the details of a Task. First define a Task singleton class with an unapply method.
| | object Task { |
| | def unapply(taskInfo : String) = { |
| | val parts = taskInfo.split("---") |
| | if(parts.size != 2) None else Some(parts(0), parts(1)) |
| | } |
| | } |
The unapply method expects the given string to contain one delimiter —, and if the parameter conforms to that, unapply returns as a tuple the two parts before and after this delimiter. If the format does not conform, it returns a None, indicating a failure of extraction. The unapply method can signal a failure by returning ...
Read now
Unlock full access