aboutsummaryrefslogtreecommitdiff
path: root/lessons/src/lesson034_record_updates
diff options
context:
space:
mode:
Diffstat (limited to 'lessons/src/lesson034_record_updates')
-rw-r--r--lessons/src/lesson034_record_updates/code.gleam15
-rw-r--r--lessons/src/lesson034_record_updates/text.html12
2 files changed, 27 insertions, 0 deletions
diff --git a/lessons/src/lesson034_record_updates/code.gleam b/lessons/src/lesson034_record_updates/code.gleam
new file mode 100644
index 0000000..ed7b45b
--- /dev/null
+++ b/lessons/src/lesson034_record_updates/code.gleam
@@ -0,0 +1,15 @@
+import gleam/io
+
+pub type SchoolPerson {
+ Teacher(name: String, subject: String, floor: Int, room: Int)
+}
+
+pub fn main() {
+ let teacher1 = Teacher(name: "Mr Dodd", subject: "ICT", floor: 2, room: 2)
+
+ // Use the update syntax
+ let teacher2 = Teacher(..teacher1, subject: "PE", room: 6)
+
+ io.debug(teacher1)
+ io.debug(teacher2)
+}
diff --git a/lessons/src/lesson034_record_updates/text.html b/lessons/src/lesson034_record_updates/text.html
new file mode 100644
index 0000000..f23d7cd
--- /dev/null
+++ b/lessons/src/lesson034_record_updates/text.html
@@ -0,0 +1,12 @@
+<p>
+ The record update syntax can be used to create a new record from an existing
+ one of the same type, but with some fields changed.
+</p>
+<p>
+ The accessor syntax can only be used for fields that are in the same position
+ and have the same type for all variants of the custom type.
+</p>
+<p>
+ Gleam is an immutable language, so using the record update syntax does not
+ mutate or otherwise change the original record.
+</p>