aboutsummaryrefslogtreecommitdiff
path: root/src/content/chapter4_standard_library/lesson04_option_module
diff options
context:
space:
mode:
authorLouis Pilfold <louis@lpil.uk>2024-01-03 19:48:25 +0000
committerLouis Pilfold <louis@lpil.uk>2024-01-03 19:48:25 +0000
commit51a1fcbd0e42b25238877d3ad05d1690e8bc1553 (patch)
tree36e8f8ad1ace4874a9c7515a8d381c5616f52d46 /src/content/chapter4_standard_library/lesson04_option_module
parentf3eaea54080300a85e7acb63a38b0bf5f7c6b531 (diff)
downloadtour-51a1fcbd0e42b25238877d3ad05d1690e8bc1553.tar.gz
tour-51a1fcbd0e42b25238877d3ad05d1690e8bc1553.zip
More stdlib
Diffstat (limited to 'src/content/chapter4_standard_library/lesson04_option_module')
-rw-r--r--src/content/chapter4_standard_library/lesson04_option_module/code.gleam14
-rw-r--r--src/content/chapter4_standard_library/lesson04_option_module/text.html16
2 files changed, 30 insertions, 0 deletions
diff --git a/src/content/chapter4_standard_library/lesson04_option_module/code.gleam b/src/content/chapter4_standard_library/lesson04_option_module/code.gleam
new file mode 100644
index 0000000..eb60001
--- /dev/null
+++ b/src/content/chapter4_standard_library/lesson04_option_module/code.gleam
@@ -0,0 +1,14 @@
+import gleam/io
+import gleam/option.{type Option, None, Some}
+
+pub type Person {
+ Person(name: String, pet: Option(String))
+}
+
+pub fn main() {
+ let person_with_pet = Person("Al", Some("Nubi"))
+ let person_without_pet = Person("Maria", None)
+
+ io.debug(person_with_pet)
+ io.debug(person_without_pet)
+}
diff --git a/src/content/chapter4_standard_library/lesson04_option_module/text.html b/src/content/chapter4_standard_library/lesson04_option_module/text.html
new file mode 100644
index 0000000..881ea94
--- /dev/null
+++ b/src/content/chapter4_standard_library/lesson04_option_module/text.html
@@ -0,0 +1,16 @@
+<p>
+ Values in Gleam are not nullable, so the
+ <a href="https://hexdocs.pm/gleam_stdlib/gleam/option.html"
+ ><code>gleam/option</code></a
+ >
+ standard library module defines Gleam's <code>Oction</code> type, which can be
+ used to represent a value that is either present or absent.
+</p>
+
+<p>
+ The option type is very similar to the result type, but it does not have an
+ error value. Some languages have functions return an option when there is no
+ extra error detail to give, but Gleam always uses result. This makes all
+ failible functions consistent and removes any boilerplate that would be
+ required when mixing functions that use each type.
+</p>