aboutsummaryrefslogtreecommitdiff
path: root/docs/structs.md
diff options
context:
space:
mode:
authorBruce Hill <bruce@bruce-hill.com>2024-08-19 00:23:02 -0400
committerBruce Hill <bruce@bruce-hill.com>2024-08-19 00:23:02 -0400
commit67e8f2dea0d4eec20a839d47f1fa6302a4a5f733 (patch)
tree3f9d28687b6ac824b5676c963ef9964ac4857c4a /docs/structs.md
parent8363d53bd27c621cb342fea15736a3b11231f2a4 (diff)
Move docs into one folder
Diffstat (limited to 'docs/structs.md')
-rw-r--r--docs/structs.md39
1 files changed, 39 insertions, 0 deletions
diff --git a/docs/structs.md b/docs/structs.md
new file mode 100644
index 00000000..4ab78fed
--- /dev/null
+++ b/docs/structs.md
@@ -0,0 +1,39 @@
+# Structs
+
+In Tomo, you can define your own structs, which hold members with arbitrary
+types that can be accessed by fields:
+
+```tomo
+struct Foo(name:Text, age:Int)
+...
+>> my_foo := Foo("Bob", age=10)
+= Foo(name="Bob", age=10)
+>> my_foo.name
+= "Bob"
+```
+
+Structs are value types and comparisons on them operate on the member values
+one after the other.
+
+## Namespaces
+
+Structs can define their own methods that can be called with a `:` or different
+values that are stored on the type itself.
+
+```tomo
+struct Foo(name:Text, age:Int):
+ oldest := Foo("Methuselah", 969)
+
+ func greet(f:Foo):
+ say("Hi my name is $f.name and I am $f.age years old!")
+
+ func get_older(f:&Foo):
+ f.age += 1
+...
+my_foo := Foo("Alice", 28)
+my_foo:greet()
+my_foo:get_older()
+```
+
+Method calls work when the first argument is the struct type or a pointer to
+the struct type.