Skip to content

Commit

Permalink
Update content for Gleam v1.6.0
Browse files Browse the repository at this point in the history
  • Loading branch information
lpil committed Nov 20, 2024
1 parent 2e19540 commit be05b9e
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,5 @@ pub fn main() {

io.debug(teacher.name)
io.debug(student.name)
// io.debug(teacher.subject)
// io.debug(student.subject)
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,18 @@
contained values from a custom type record.
</p>
<p>
The accessor syntax can only be used for fields with the same name that are in
the same position and have the same type for all variants of the custom type.
The accessor syntax can always be used for fields with the same name that are
in the same position and have the same type for all variants of the custom
type. Other fields can only be accessed when the compiler can tell which
variant the value is, such after pattern matching in a `case` expression.
</p>
<p>
The <code>name</code> field is in the first position and has type
<code>String</code> for all variants, so it can be accessed.
</p>
<p>
The <code>subject</code> field is absent on the <code>Student</code> variant,
so it cannot be used on any variant of type <code>SchoolPerson</code>.
Uncomment the <code>teacher.subject</code> line to see the compile error from
so it cannot be used on all values of type <code>SchoolPerson</code>.
Uncomment the <code>student.subject</code> line to see the compile error from
trying to use this accessor.
</p>
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,20 @@ pub type IceCream {
}

pub fn main() {
let lucy = Starfish("Lucy", "Pink")
let favourite_ice_cream = IceCream("strawberry")
handle_fish(Starfish("Lucy", "Pink"))
handle_ice_cream(IceCream("strawberry"))
}

case lucy {
fn handle_fish(fish: Fish) {
case fish {
Starfish(_, favourite_color) -> io.debug(favourite_color)
Jellyfish(name, ..) -> io.debug(name)
}
}

fn handle_ice_cream(ice_cream: IceCream) {
// 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
let IceCream(flavour) = ice_cream
io.debug(flavour)
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
<p>
It is possible to pattern match on a record, this allows for the extraction
of multiple field values from a record into distinct variables, similar to matching on a tuple or a list.
It is possible to pattern match on a record, this allows for the extraction of
multiple field values from a record into distinct variables, similar to
matching on a tuple or a list.
</p>
<p>
The <code>let</code> keyword can only match on single variant custom types. For types with more variants
a case expression must be used.
The <code>let</code> keyword can only match on single variant custom types, or
when the variant is know, such as after pattern matching with a case
expression.
</p>
<p>
It is possible to use underscore <code>_</code> or the spread syntax <code>..</code> to
discard fields that are not required.
</p>
It is possible to use underscore <code>_</code> or the spread syntax
<code>..</code> to discard fields that are not required.
</p>

0 comments on commit be05b9e

Please sign in to comment.