aboutsummaryrefslogtreecommitdiff
path: root/notes/optional_types.txt
blob: 66f192c414ccf34b0d707f38091f261f53cd258a (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

Optional types
==============

Optional types/variables could go at two locations:

* At the type, such as:  `?String name`
* At the variable/field: `String name?`

Note that the latter does not support optionals in generic parameters.
The choice also affects changeability/mutability qualifiers.

Syntax examples:

    # At type
    ?String name
    ?Writer! wr
    ?String last_url!
    ?List Int items

    # At variable
    String name?
    Writer! wr?
    String last_url!?
    List Int items?

    # or should `?` come before `!`
    String last_url?!


Optional types in type-generic elements
---------------------------------------

It would be tricky (but not impossible) to support optional types in type
parameters of generic types.

The problem is that the `None` value can appear both at the type parameter
level (`T?`, where `T` is of an unknown type), and at the usage level
`List ?String`.

This could possibly be implemented by counting the "level" where the `None`
value originates from. But is that possible to do with multiple levels of
generics? Such as the following:

    StringMap List List String

I think it should be possible. I don't think there could be more than a 2-
level ambiguity, because each nesting level of the generic type has its own
totally separate generic storage slot.

So it should be sufficient to have 2 different `None` values:

* `None`
* `GenericNone` / `EmptySlot` / `BlankSlot`
    - This one could have be implemented separate value,
      in addition to the `NULL` value. Perhaps `1` (or all ones).

This would also mean that the varstate tracking needs to be aware of 3 cases:
EmptySlot, None, and value-present.

Note that an EmptySlot means that the generic value does not exist at all,
so it isn't None it not even valid (similar to an unassigned variable).