Type directed array disambiguation
This RFC proposes extending the existing constructor-based type-directed disambiguation mechanism to array literal syntax, in both expressions and patterns. Right now, it would serve only to give literal syntax to floatarrays, but other array constructs seem likely soon (such as immutable arrays or uniform arrays).
There's some previous discussion under https://github.com/ocaml/ocaml/pull/616, which included array literal disambiguation as part of a larger proposal that included various additional syntactic conveniences (e.g. allowing disambiguation for .() and .()<-, and also using scope to resolve the [||] syntax.)
Regarding the current proposal: I'm in favour. The benefits are fairly limited, but it's forward-compatible with various more interesting things that we might want to do later (e.g. immutable arrays).
Out of curiosity, I gave it a try: https://github.com/nojb/ocaml/commit/c124bfadf52dee457770c6693cff9dd0ff58c07d. Not sure if the approach is exactly right (comments welcome!), but worked well enough for an experiment:
OCaml version 5.3.0+dev0-2023-12-22
Enter #help;; for help.
# let x = [|1.3; 4.5|];;
val x : float array = [|1.3; 4.5|]
# x.(1);;
- : float = 4.5
# let x : floatarray = [|1.3; 4.5|];;
val x : floatarray = <abstr>
# Float.Array.get x 1;;
- : float = 4.5
# let f = function [|a; b|] -> a + b;;
val f : int array -> int = <fun>
# f [|1;3|];;
- : int = 4
# let f = function [|a; b|] -> a +. b;;
val f : float array -> float = <fun>
# f [|1.;3.|];;
- : float = 4.
# let f : floatarray -> _ = function [|a; b|] -> a +. b;;
val f : floatarray -> float = <fun>
# f [|1.;3.|];;
- : float = 4.
#
@nojb would you want to open a PR with that implementation? Maybe we can just get this one in. Thanks for taking a stab!
@nojb would you want to open a PR with that implementation? Maybe we can just get this one in. Thanks for taking a stab!
Yes, I will open a PR soon and it can be discussed there. Thanks.
@nojb would you want to open a PR with that implementation? Maybe we can just get this one in. Thanks for taking a stab!
Yes, I will open a PR soon and it can be discussed there. Thanks.
See https://github.com/ocaml/ocaml/pull/13340.
This has now been merged in the compiler. Do we close? Do we merge this? I don't know the protocol.
We don't have a good protocol for RFCs, the value is in the discussions I think. Merging would make sense, as it keeps a track record of RFCs we have accepted (in practice, if not formally).