aboutsummaryrefslogtreecommitdiff
path: root/notes/multiple_returns.txt
blob: 93350bdf25e9c7349616b2913cbac2ded76dfbe2 (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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168

Multiple return values
======================

Syntax for returning multiple values is trivial, it could be one of
the following:

    return 1 2    # this matches how function calls look like
    return 1, 2
    return [ 1 2 ]

But how about the following:

* How to define such functions: See func_sections.txt
* How to receive multiple return values to existing variables?
* How to receive multiple return values to new variables?
    - This can lead to quite long lines
    - But it could also be convenient.
    - But in C89 and Pascal you had to define the variables on separate
      lines in many cases and it worked fine.
* How to return all return values as-is?

Syntax test
-----------

Syntax 1:

    Int e1
    Bool e2

    [ e1 e2 ] = somefunc

    [ Int n1 Bool n2 ] = somefunc

    return somefunc

Syntax 2:

    Int e1
    Bool e2

    to e1
    to e2
    somefunc

    to Int n1
    to Bool n2
    somefunc

    return somefunc

Syntax 3:

    # Using ,= to make it less confusing for C, Java or C++ programmers
    Int e1
    Bool e2
    e1, e2 ,= somefunc
    Int n1, Bool n2 ,= somefunc
    return somefunc

Syntax 4:

    Int e1
    Bool e2
    e1 =, e2 = somefunc
    Int n1=, Bool n2 = somefunc
    return somefunc

Syntax 5:

    Int e1
    Bool e2
    e1, e2 ]= somefunc
    Int n, Bool n2 ]= somefunc
    return somefunc

Syntax 6:

    # Skip support for declarations of multiple variables on 1 line.
    Int e1
    Bool e2
    e1, e2 = somefunc
    Int n1
    Bool n2
    n1, n2 = somefunc
    return somefunc

Syntax 7:

    Int e1
    Bool e2
    e1, e2 = somefunc
    Int n1,
    Bool n2,
    = somefunc
    return somefunc

Syntax 8:

    # This leads to an ambiguity with array element assignment,
    # but maybe it can be solved somehow?
    Int e1
    Bool e2

    e1 e2 = somefunc
    arr i = somefunc

    return somefunc

Syntax 9:

    Int e1
    Bool e2

    set e1 e2 = somefunc

    return somefunc

Alternative solutions
---------------------

Alternative solution 1: Error/Context objects

    Int e1
    Bool e2

    e1 = somefunc
    e2 = ctx.get_xxx

    Int n1 = somefunc
    Bool n2 = ctx.get_xxx

    return somefunc    # context is available to the caller

Alternative solution 2: Make ad-hoc definitions of structs easy

    # Syntax 1: without names
    Pair Int Bool ep
    ep = somefunc
    Pair Int Bool np = somefunc
    return somefunc

    # Syntax 2: also without names
    Int/Bool ep
    ep = somefunc
    Int/Bool np = somefunc
    return somefunc

    # Syntax 3: also without names
    # a comma is still required to distinguish it from generic types
    (Int, Bool) ep
    ep = somefunc
    (Int, Bool) np = somefunc
    return somefunc

    # Syntax 4: record definitions, with names
    record SomeFuncReturn
        Int x
        Bool b
    end

    SomeFuncReturn ep
    ep = somefunc
    SomeFuncReturn np = somefunc
    return somefunc

Alternative solution 3: Don't support multiple-return values

Classes are quite easy to define in SLUL, so it might not be necessary.