diff options
author | Louis Pilfold <louis@lpil.uk> | 2024-02-20 12:20:16 +0000 |
---|---|---|
committer | Louis Pilfold <louis@lpil.uk> | 2024-02-20 12:20:16 +0000 |
commit | 938f49fc7835501192ccc7db7c1ab3174f050b34 (patch) | |
tree | 3e515fa15b6812df9947df80b8c16afd20faa436 /src/content/chapter5_advanced_features/lesson04_let_assert | |
parent | e7d45ec089262e8b6731ddf87e65a06f4de07841 (diff) | |
download | tour-938f49fc7835501192ccc7db7c1ab3174f050b34.tar.gz tour-938f49fc7835501192ccc7db7c1ab3174f050b34.zip |
Let assert
Diffstat (limited to 'src/content/chapter5_advanced_features/lesson04_let_assert')
-rw-r--r-- | src/content/chapter5_advanced_features/lesson04_let_assert/code.gleam | 16 | ||||
-rw-r--r-- | src/content/chapter5_advanced_features/lesson04_let_assert/en.html | 15 |
2 files changed, 31 insertions, 0 deletions
diff --git a/src/content/chapter5_advanced_features/lesson04_let_assert/code.gleam b/src/content/chapter5_advanced_features/lesson04_let_assert/code.gleam new file mode 100644 index 0000000..2ba907a --- /dev/null +++ b/src/content/chapter5_advanced_features/lesson04_let_assert/code.gleam @@ -0,0 +1,16 @@ +import gleam/io + +pub fn main() { + let a = unsafely_get_first_element([123]) + io.debug(a) + + let b = unsafely_get_first_element([]) + io.debug(b) +} + +pub fn unsafely_get_first_element(items: List(a)) -> a { + // This will panic if the list is empty. + // A regular `let` would not permit this partial pattern + let assert [first, ..] = items + first +} diff --git a/src/content/chapter5_advanced_features/lesson04_let_assert/en.html b/src/content/chapter5_advanced_features/lesson04_let_assert/en.html new file mode 100644 index 0000000..29fd06c --- /dev/null +++ b/src/content/chapter5_advanced_features/lesson04_let_assert/en.html @@ -0,0 +1,15 @@ +<p> + <code>let assert</code> is the final way to intentionally crash your Gleam + program. It is similar to the <code>panic</code> keyword in that it crashes + when the program has reached a point that should never be reached. +</p> +<p> + <code>let assert</code> is similar to <code>let</code> in that it is a way to + assign values to variables, but it is different in that the pattern can be + <i>partial</i>. The pattern does not need to match every possible value of the + type being assigned. +</p> +<p> + Like <code>panic</code> this feature should be used sparingly, and likely not + at all in libraries. +</p> |