aboutsummaryrefslogtreecommitdiff
path: root/docs/optionals.md
blob: 1414ca6a895efea20a46490ac10d2b2c6f9dedd0 (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
# Optional Values

A very common use case is values that may or may not be present. You could
represent this case using enums like so:

```tomo
enum MaybeInt(AnInt(x:Int), NoInt)

func maybe_takes_int(maybe_x:MaybeInt):
    when maybe_x is AnInt(x):
        say("Got an int: $x")
    else:
        say("Got nothing")
```

However, it's overly onerous to have to define a separate type for each
situation where you might want to not have a value. Instead, Tomo has
built-in support for optional types:

```
func maybe_takes_int(x:Int?):
    if x:
        say("Got an int: $x")
    else:
        say("Got nothing")
```

This establishes a common language for talking about optional values without
having to use a more generalized form of `enum` which may have different naming
conventions and which would generate a lot of unnecessary code. 

In addition to using conditionals to check for null values, you can also use
`:or_else(fallback)` or `:or_fail()`:

```tomo
maybe_x := 5?
>> maybe_x:or_else(-1)
= 5 : Int
>> maybe_x:or_fail()
= 5 : Int

maybe_x = !Int
>> maybe_x:or_else(-1)
= -1 : Int
>> maybe_x:or_fail()
# Failure!
```