aboutsummaryrefslogtreecommitdiff
path: root/tour/literals.sloth
blob: e2be0d0ea7ef18b51cfc89795166863d80a99020 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# Literals
val number = 85; #TODO: Decide on default integer type
val number = 85.0; # f64 is the default float type

val number: u16 = 27; # If you want more control over memory usage you can specify a integer type
val number: u16 = 27u16;
val number: u16 = 0x1B;
val number: u16 = 0x1Bu16;

val number: BigInt = BigInt::from(73);     #TODO: naming
val number: BigFloat = BigFloat::from(73); #TODO: naming

val chars: char = ' ';

val strings: String = "Normal string";
val strings: String = "Formated strings ${number}";
val strings: String = """String literals""";

val regex: Regex = /[0-9A-F]/;

val list: List<i32> = [1, 2, 3, 2];
val sets: Set<i32> = {1, 2, 3, 2};

val maps = { 
    "foo": 48,
    "bar": 97, 
};

val maps: Map<String, i32> = { 
    "foo": 48,
    "bar": 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<T>
if 'p' in "Apple" {}   # impl Contains<char> for String
if "ppl" in "Apple" {} # impl Contains<String> for String
if /[0-9]/ in "24" {}  # impl Contains<Regex> 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<i32>
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];
val rhs = [4, 5, 6];
val combined_list = [..lhs, ..rhs, 2, 4, 6];
val combined_sets = {..lhs, ..rhs, 2, 4, 6};