Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce single-variant record custom type first #140

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 7 additions & 10 deletions src/content/chapter3_data_types/lesson02_records/code.gleam
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@
import gleam/io

pub type SchoolPerson {
Teacher(name: String, subject: String)
Student(String)
pub type Person {
Person(name: String, age: Int, needs_glasses: Bool)
}

pub fn main() {
let teacher1 = Teacher("Mr Schofield", "Physics")
let teacher2 = Teacher(name: "Miss Percy", subject: "Physics")
let student1 = Student("Koushiar")
let student2 = Student("Naomi")
let student3 = Student("Shaheer")
let amy = Person("Amy", 26, True)
let jared = Person(name: "Jared", age: 31, needs_glasses: True)
let tom = Person("Tom", 28, needs_glasses: False)

let school = [teacher1, teacher2, student1, student2, student3]
io.debug(school)
let friends = [amy, jared, tom]
io.debug(friends)
}
4 changes: 4 additions & 0 deletions src/content/chapter3_data_types/lesson02_records/en.html
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,7 @@
It is common to have a custom type with one variant that holds data, this is
the Gleam equivalent of a struct or object in other languages.
</p>
<p>
When defining custom types with one variant, the single variant is often named the
same as the custom type, although it doesn't have to be.
</p>
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
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.
variant the value is, such after pattern matching in a <code>case</code> expression.
</p>
<p>
The <code>name</code> field is in the first position and has type
Expand Down
Loading