Skip to content

Commit c7a4158

Browse files
authored
Update singleton-objects.md
Added a note about 'Option', 'Some', and 'None'
1 parent 98f7516 commit c7a4158

File tree

1 file changed

+8
-0
lines changed

1 file changed

+8
-0
lines changed

_tour/singleton-objects.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,14 @@ scalaCenterEmail match
196196

197197
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.
198198

199+
A note about `Option`, `Some`, and `None` in the code above:
200+
* `Option` is a data type which allows for optionality. It has two cases: `Some` and `None`
201+
* `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.
202+
* `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.
203+
* The `Option` return type can then be used in a match/case:
204+
* 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.
205+
* 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.
206+
199207
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.
200208

201209
## Notes for Java programmers ##

0 commit comments

Comments
 (0)