5 # Any type can be optional, which means it will either be
6 # `none` or have a value. You can declare an optional variable
7 # and initialize it to `none` like this:
12 # Optional variables can be either `none` or a value of that type:
17 # Some functions return optional values.
18 # Int.parse(text:Text -> Int?) is a function that returns
19 # an integer value found in the text, or `none` if no integer
21 assert Int.parse("123") == ???
22 assert Int.parse("blah") == ???
24 # You can check if a value exists with `if`:
27 # Inside this condition, `n` is known to be non-none
31 # Optionals are useful for handling missing data:
38 assert greeting == ???
40 # Optional values can be converted to non-optional using `or`
41 assert Int.parse("blah") or 0 == ???
43 # They can also be converted using the `!` operator, which
44 # will give an error if a non-none value is encountered:
45 assert add(Int.parse("123")!, 1) == ???
47 # If you index into a list, the value is optional because
48 # you might have used an index that isn't in the list:
51 assert list[999] == none
53 # So, it's common to see `list[i]!` to force the value to
54 # be non-optional or error if you made a mistake:
55 assert list[3]! == ???
57 func add(x:Int, y:Int -> Int)