From 35418382a0cc1388cf8f2dc034b9c2ed82b60535 Mon Sep 17 00:00:00 2001 From: Giacomo Cavalieri Date: Thu, 5 Sep 2024 16:10:37 +0200 Subject: [PATCH] Add example for single variant record let structuring --- .../lesson04_record_pattern_matching/code.gleam | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/content/chapter3_data_types/lesson04_record_pattern_matching/code.gleam b/src/content/chapter3_data_types/lesson04_record_pattern_matching/code.gleam index 1c58b0d..3ef27b2 100644 --- a/src/content/chapter3_data_types/lesson04_record_pattern_matching/code.gleam +++ b/src/content/chapter3_data_types/lesson04_record_pattern_matching/code.gleam @@ -5,11 +5,21 @@ pub type Fish { Jellyfish(name: String, jiggly: Bool) } +pub type IceCream { + IceCream(flavour: String) +} + pub fn main() { let lucy = Starfish("Lucy", "Pink") + let favourite_ice_cream = IceCream("strawberry") case lucy { Starfish(_, favourite_color) -> io.debug(favourite_color) Jellyfish(name, ..) -> io.debug(name) } + + // if the custom type has a single variant you can + // destructure it using `let` instead of a case expression! + let IceCream(flavour) = favourite_ice_cream + io.debug(flavour) }