aboutsummaryrefslogtreecommitdiff
path: root/documentation/tour/traits.sloth
diff options
context:
space:
mode:
authorCody <cody@codyq.dev>2023-03-30 02:44:54 -0500
committerCody <cody@codyq.dev>2023-03-30 02:44:54 -0500
commit2970520a9592b5c6d45291f54073552a474b71b4 (patch)
treef42ecc1be0989367cf7c70d0b7909bac0b86904e /documentation/tour/traits.sloth
parentbb95375f8b24141bf7dfe5a8b1bba5c995f61253 (diff)
downloadsloth-2970520a9592b5c6d45291f54073552a474b71b4.tar.gz
Restructure
Diffstat (limited to 'documentation/tour/traits.sloth')
-rw-r--r--documentation/tour/traits.sloth34
1 files changed, 34 insertions, 0 deletions
diff --git a/documentation/tour/traits.sloth b/documentation/tour/traits.sloth
new file mode 100644
index 0000000..80319de
--- /dev/null
+++ b/documentation/tour/traits.sloth
@@ -0,0 +1,34 @@
+# Much like Rust's traits or Haskell's type classes sloth uses a trait system for
+# polymorphism.
+trait BasicTrait {
+ fn add() -> i32;
+}
+
+trait AddAssign: Add {
+ fn add_assign(value: i32, rhs: i32) -> i32;
+}
+
+trait Add {
+ fn add(lhs: i32, rhs: i32) -> i32;
+
+ default impl AddAssign {
+ fn add_assign(value: i32, rhs: i32) -> i32 {
+ return add(value, rhs);
+ }
+ }
+}
+
+# In order to make implementing traits easier you can automatically derive traits.
+# Types will implicitly derive from Debug, Copy, Eq and Ord if possible.
+type Person = {
+ name: String,
+ age: i32,
+ hobbies: Set<String>,
+};
+
+# You can easily derive from more traits using the `derive` keyword.
+type Person derives Serialize, Deserialize = {
+ name: String,
+ age: i32,
+ hobbies: Set<String>,
+};