Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions _tour/singleton-objects.md
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,14 @@ scalaCenterEmail match

The `object Email` contains a factory `fromString` which creates an `Email` instance from a String. We return it as an `Option[Email]` in case of parsing errors.

A note about `Option`, `Some`, and `None` in the code above:
* `Option` is a data type which allows for optionality. It has two cases: `Some` and `None`
* `Some` above represents a match: the emailString, when split by a @, returns an array with two components. This allows creation of a valid instance of class Email.
* `None` above represents no match: the emailString, when split by a @, did not return an array with two components. It could not allow creation of a valid instance of class Email.
* The `Option` return type can then be used in a match/case:
* For a `Some` result, the match knows the return type is a valid instance of Email and can populate the value email with that instance.
* For a `None` result, the match knows the return type is not a valid instance of Email, so it can print an appropriate error message.

Note: If a class or object has a companion, both must be defined in the same file. To define companions in the REPL, either define them on the same line or enter `:paste` mode.

## Notes for Java programmers ##
Expand Down