From c7a41586a2fdad2e5ba59cb12e5a13378536c359 Mon Sep 17 00:00:00 2001 From: Erik Pohl <89852209+ErikPohl444@users.noreply.github.com> Date: Fri, 13 Dec 2024 10:57:04 -0500 Subject: [PATCH 1/2] Update singleton-objects.md Added a note about 'Option', 'Some', and 'None' --- _tour/singleton-objects.md | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/_tour/singleton-objects.md b/_tour/singleton-objects.md index 47a67ee175..431347bdb0 100644 --- a/_tour/singleton-objects.md +++ b/_tour/singleton-objects.md @@ -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 ## From 6932023877c0b1ac1478fcb192cff681ddbc5ba4 Mon Sep 17 00:00:00 2001 From: Erik Pohl <89852209+ErikPohl444@users.noreply.github.com> Date: Mon, 16 Dec 2024 08:42:35 -0500 Subject: [PATCH 2/2] Update _tour/singleton-objects.md Thank you for the edit. Accepted. Co-authored-by: adpi2 --- _tour/singleton-objects.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/_tour/singleton-objects.md b/_tour/singleton-objects.md index 431347bdb0..828d49a38a 100644 --- a/_tour/singleton-objects.md +++ b/_tour/singleton-objects.md @@ -201,8 +201,8 @@ A note about `Option`, `Some`, and `None` in the code above: * `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. + * For a `Some` result, the match knows the returned value is an instance of `Email`, so it can access the inner `username` and `domainName`. + * For a `None` result, the match knows the returned value is not an instance of `Email`, so it prints 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.