diff options
Diffstat (limited to 'tour/types.sloth')
| -rw-r--r-- | tour/types.sloth | 91 |
1 files changed, 0 insertions, 91 deletions
diff --git a/tour/types.sloth b/tour/types.sloth deleted file mode 100644 index 8af0856..0000000 --- a/tour/types.sloth +++ /dev/null @@ -1,91 +0,0 @@ -# Structure Type -type Person = { - name: String, - age: i32, - hobbies: Set<String>, - grades: Map<String, i32>, -}; - -val cody = Person( - name: "Cody Q", - age: 17, - hobbies: { - "Computer Science", - "Cybersecurity", - }, - grades: { - "Computer Science": 100, - "Mathmatics": 96, - "Physics": 93, - "English": 78, - }, -); - -# Tuple Type -type Position = (i32, i32); - -# Tagged Union Type -type Option<T> = - | None - | Some = T; - -type Class = Archer | Mage | Tank; - -type Operation = - | Add = (Operation, Operation) - | Lit = i32; - -# Untagged Union Type -# -# Unlike tagged unions if all variants of a untagged union implement a specific -# trait you will be able to call those trait methods without getting the variant -# first. -type Untagged = i32 + String; - -# Type Alias -type OptionPos = Option<Position>; - -# You can define untagged union types and tuple types inline without a name, this -# may be useful for one-off types only used by a single function. -val example: String + i32 = 52; -val example: (i32, i32) = (5, 5); - -# Functions can be associated with types using 'impl' blocks. -type Color = (f64, f64, f64); - -impl Color { - pub const BLACK = Color(0.0, 0.0, 0.0); - pub const WHITE = Color(1.0, 1.0, 1.0); - pub const RED = Color(1.0, 0.0, 0.0); - pub const GREEN = Color(0.0, 1.0, 0.0); - pub const BLUE = Color(0.0, 0.0, 1.0); - - ## Get a color from red green and blue - pub fn rgb(red: u8, green: u8, blue: u8) -> Color { - Color(red / 255.0, green / 255.0, blue / 255.0) - } - - ## Get a color from a hue, saturation and value - pub fn hsv(hue: f64, saturation: f64, value: f64) -> Color { - fn c(value: f64) -> u8 { - (value * 255) as u8 - } - - val h = (hue * 6).floor() as i32; - - val f = hue * 6 - h; - val p = value * (1 - saturation); - val q = value * (1 - f * saturation); - val t = value * (1 - (1 - f) * saturation); - - return match h { - 0 => Color(c(value), c(t), c(p)), - 1 => Color(c(q), c(value), c(p)), - 2 => Color(c(p), c(value), c(t)), - 3 => Color(c(p), c(q), c(value)), - 4 => Color(c(t), c(p), c(value)), - 5 => Color(c(value), c(p), c(q)), - _ => Color::BLACK, - } - } -} |
