diff options
| author | Cody <cody@codyq.dev> | 2022-12-15 13:23:48 -0600 |
|---|---|---|
| committer | Cody <cody@codyq.dev> | 2022-12-15 13:23:48 -0600 |
| commit | bddb011df4999f7ffeeddf6a4b66e2da6ab19ea0 (patch) | |
| tree | 874d175f352f1a4688e7e62d1f9222a192ae9bff /tour/traits.sloth | |
| download | sloth-bddb011df4999f7ffeeddf6a4b66e2da6ab19ea0.tar.gz | |
Initial language designs & lexer from crafting interpreters
The very initial language designs I came up with for Sloth. Likely
contains inconsistencies and definitely contains things that will be
changed in the future. This is basically just a dump of every idea I've
had for the language thus far.
As for the lexer right now it is heavily based on the one from the
Crafting Interpretrs book and doesn't yet parse Sloth grammar.
Diffstat (limited to 'tour/traits.sloth')
| -rw-r--r-- | tour/traits.sloth | 34 |
1 files changed, 34 insertions, 0 deletions
diff --git a/tour/traits.sloth b/tour/traits.sloth new file mode 100644 index 0000000..80319de --- /dev/null +++ b/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>, +}; |
