From a653a6602fe5ae5eb4739755db7b34bc92ecdadf Mon Sep 17 00:00:00 2001 From: Cody Date: Sun, 29 Jan 2023 20:58:04 -0600 Subject: More stuffs --- tour/literals.sloth | 48 ++++++++++++++++++++++++++++++++++++++++-------- tour/modules.sloth | 49 ++++++++++++++++++++++++++++++++++++++++--------- tour/types.sloth | 16 ++++++++++++++-- 3 files changed, 94 insertions(+), 19 deletions(-) (limited to 'tour') diff --git a/tour/literals.sloth b/tour/literals.sloth index 4aa4533..e2be0d0 100644 --- a/tour/literals.sloth +++ b/tour/literals.sloth @@ -31,14 +31,46 @@ val maps: Map = { "bar": 97, }; -# `value?` Can be used to bubble up an Option or Result -# `value!` Can be used to panic on None or Error - -maps["foo"] # Option -maps["foo"]! # 48 -maps["foo"]? # 48 - Caller of function is responsible for None case -maps.keys() # ["foo", "bar"] -maps.values() # [48, 97] +# Types can be 'any' and be assigned to anything +var anything: any = "I'm a string right now"; +anything = 53; +anything = "I was a number- but now I'm a string again"; + +# You can use the `is` keyword to check if a type is something +if anything is String { + # Now I can call functions that take a String + anything.split('-'); +} + +# TODO: HMMMMMMM- +if anything is Some(it) {} + +# You can use the `in` keyword to check if something is in a collection +if "hello" in ["hello", "hola"] { + # +} + +# ... or a range +if 5 in 2..17 { + # +} + +# ... or anything that implements Contains +if 'p' in "Apple" {} # impl Contains for String +if "ppl" in "Apple" {} # impl Contains for String +if /[0-9]/ in "24" {} # impl Contains for String + +# `value!` Can be used to bubble up an Option or Result +# `value!!` Can be used to panic on None or Error +# `value?` Can be used to optionally chain +# `value ?: 0` Can be used to provide a default + +maps["foo"] # Option +maps["foo"]!! # 48 - Panics in None case +maps["foo"]! # 48 - Caller of function is responsible for None case +maps["foo"]?.signum() ?: 0 # 1 - Provide a default for None case +maps.keys() # ["foo", "bar"] +maps.values() # [48, 97] # Spreading val lhs = [1, 2, 3]; diff --git a/tour/modules.sloth b/tour/modules.sloth index 1523753..b91261e 100644 --- a/tour/modules.sloth +++ b/tour/modules.sloth @@ -24,18 +24,49 @@ use extern "git://github.com/CatDevz/AdventOfCode.git" as spookylib; use extern "ftp://codyq.dev/libs/spookylib.sloth" as spookylib; use extern "https://codyq.dev/libs/spookylib.sloth" as spookylib; -# In order to use modules or members of modules without quantifying the entire -# path you must include them using a `use` statement. Star imports do not exist -# because they fucking suck. +# In sloth files will automatically become modules. Files named `mod.sloth` will +# become the module for the directory. If you have a project with multiple files +# the root module will be `mod.sloth`. Anything exposed in this can just strait up +# be used. # +# If no `mod.sloth` exists, for example when running single file scripts, it will +# be trated as if there is one but its empty. + +# /mod.sloth +pub fn fib(n: i32) -> i32 { + match n { + 0 | 1 => n, + _ => fib(n - 1) + fib(n - 2), + } +} + +# /foo.sloth +use fib; # TODO: + +fib(5); # Works because everything inside of mod.sloth is fair game + +# This means if you want an extern library to be available in every module you just +# need to add a `pub(pkg) use extern "lib"` + +# In order to use modules or members of modules without quantifying the entire +# path you must include them using a `use` statement. +use foo::bar; + +# Sloth will use your root module (the one used as your entrypoint) for imports. +# In order to import from modules relative to your own you must prefix the use +# with a `::` +use ::from::relative; + +# If you would live to traverse up the module tree you can use the `super` +# psudo-module. +use + +# TODO: # Sloth will automatically turn files relative to your own and directories # relative to your own with a `mod.sloth` into modules. In order to traverse -# up the module tree you can use the `super` and `pkg` psudo-modules. -# -# The super psudo-module will go up a single module in the module tree whereas -# the pkg psudo-module will go to the root module in the module tree. -use pkg::a; -use pkg::b; +# up the module tree you can use the `super` psudo-module. +use super::a; +use super::b; use std::rand::random; use std::uuid; diff --git a/tour/types.sloth b/tour/types.sloth index c7ef2f5..0fdae33 100644 --- a/tour/types.sloth +++ b/tour/types.sloth @@ -32,12 +32,24 @@ type Option = type Class = Archer | Mage | Tank; type Operation = - | Add = (Operation, Operation) - | Literal = i32; + | 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; +# 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); -- cgit v1.2.3