You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: rfcs/val_annotations.md
+53-2Lines changed: 53 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -78,9 +78,15 @@ let rev li =
78
78
79
79
## Discussion and extensions
80
80
81
+
The suggestions below are *not* intended for inclusion as part of the
82
+
RFC (they would not be implemented along with what's above), but they
83
+
serve to record and discuss alternative choices, and/or to check
84
+
compatibility with other features by showing that the feature can be
85
+
extended if desired.
86
+
81
87
### Locally abstract types in `val` declarations
82
88
83
-
We propose to extend the form `type a . ...` to work with value
89
+
One can consider extending the form `type a . ...` to work with value
84
90
annotations. For example:
85
91
86
92
```ocaml
@@ -94,7 +100,7 @@ let mem elt li =
94
100
95
101
Notice that `type a` binds the variable `a` in both the declaration and the definition.
96
102
97
-
(This extension could be left out of a first implementation of this proposal.)
103
+
This extension may not be necessary if [#12732](https://github.com/ocaml/ocaml/pull/12732) is merged. In general there are discussions around the semantics of `type a` vs `'a` that are independent of the present PR. We merely point out that `val` could scale to `type a.` if desired.
98
104
99
105
100
106
### Alternative syntax: declarations within `let` blocks
@@ -128,6 +134,7 @@ let rev li =
128
134
in loop li []
129
135
```
130
136
137
+
131
138
### Combination with `_` inference from signature
132
139
133
140
oxcaml has a work-in-progress feature where `_` can be used to elide types and module signatures in structures (in particular `.ml` files), when they are declared in the corresponding signature (in particular the `.mli` file), see https://github.com/oxcaml/oxcaml/pull/2783 . This feature is independent, but it was part of the motivation to revive the current proposal, as it can naturally be combined:
@@ -136,3 +143,47 @@ oxcaml has a work-in-progress feature where `_` can be used to elide types and m
136
143
val map : _
137
144
let rec map f li = ...
138
145
```
146
+
147
+
A more compact form specific to toplevel `let` definitions could be
148
+
considered:
149
+
150
+
```
151
+
val rec map f li = ...
152
+
```
153
+
154
+
it would both imply `val map : _` as above and could only be included
155
+
once per structure (shadowing `val` declarations with another `val` is
156
+
forbidden.)
157
+
158
+
159
+
### Tension with using `val` for forward-declaration
160
+
161
+
Jeremy Yallop points out that `val` could be considered to introduce recursion, so that
162
+
163
+
```
164
+
let rec fac n = function
165
+
| 0 -> 1
166
+
| n -> n * fac (n - 1)
167
+
```
168
+
169
+
could also be written (note the absence of `rec` below)
170
+
171
+
```
172
+
val fac : int -> int
173
+
let fac n = function
174
+
| 0 -> 1
175
+
| n -> n * fac (n - 1)
176
+
```
177
+
178
+
That interpretation of `val` is *incompatible* with the one proposed in this PR, as their semantics would differ in the following case:
179
+
180
+
```
181
+
let fac n = 0 (* first definition *)
182
+
183
+
val fac : int -> int
184
+
let fac n = function
185
+
| 0 -> 1
186
+
| n -> n * fac (n - 1) (* first definition or recursive occurrence? *)
187
+
```
188
+
189
+
Leo White proposes to use `val rec` to implicitly introduce recursion, to avoid the incompatibility between the two features. (Gabriel Scherer and Kate Deplaix alternatively proposed to require `let rec` to take `val` forward declarations into account, but Jeremy Yallop was not impressed by this proposal.)
0 commit comments