aboutsummaryrefslogtreecommitdiffhomepage
path: root/notes/constness_problem.txt
blob: 2becd8c0980bf17fc92c825ff3c9b49fee3908dc (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
80
81
82
83
84
85
86
87
88
89

The constness problem
=====================

This applies to other qualifiers than just "var".
But it mostly known for the const problems that happen in C-like languages:
The "strchr"/"memchr" and similar functions take a const but return a
non-const. That is of course not acceptable in a safe language.


Solution 1
----------
Allow functions to specify a "var" return and infer the var-ness based
on the function arguments. For example:

    func ref ByteBuffer.subrange(int low, int high) -> var ByteBuffer

    func f() {
        ByteBuffer cb = ...
        var ByteBuffer vb = ...
        # In this case, the return type loses the var-ness
        # because there is no argument that is var
        ByteBuffer cb1 = cb.subrange(1, 3)
        # In this case, the return type retains the var-ness
        ByteBuffer vb1 = vb.subrange(1, 3)
    }

(This syntax works because a function taking only non-var parameters
cannot ever return a var value. So it would be meaningless, and can
be given this alternate meaning.)

Problem: How should/can this work with multiple parameters?

    # In this case, the function should base the var-ness on
    # the implicit "this" parameter.
    func ref ByteBuffer.largest_common_range(ref ByteBuffer other)
        -> var ByteBuffer

How does this work with other qualifiers, such as "arena", "threaded"?
- The parameter can only come from the input!
- BOTH the input and output should have the qualifiers
- The compiler should eliminate qualifiers on the output
  that are not present on the input.


Solution 2
----------
Allow functions to specify a "var" return and infer the var-ness based
on a specific argument:
    
    func ref ByteBuffer.largest_common_range(ref ByteBuffer other)
        -> var(this) ByteBuffer

How does this work with other qualifiers, such as "arena", "threaded"?


Solution 3
----------
Use a special keyword to inherit ALL qualifiers:

    func ref ByteBuffer.largest_common_range(ref ByteBuffer other)
        -> quals_of(this) ByteBuffer


Regarding specific qualfiers
----------------------------
var,
writeonly,
arena,
aliased,
threaded:
- No specific considerations

own:
- An "own" parameter can ONLY take "own" values,
  and non-"own" parameters can ONLY take non-"own" values.
--> inference/inheritence of own-ness makes no sense.


Regarding output parameters
---------------------------

- Will NOT work fully with solution 1.
- With solution 3:
    "var aliased(other_param) out_param"
- With solution 3:
    "var quals_of(other_param) out_param"

Related problem:
- Qualifiers on nested references?