RFCs icon indicating copy to clipboard operation
RFCs copied to clipboard

Type directed array disambiguation

Open goldfirere opened this issue 1 year ago • 5 comments

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).

goldfirere avatar Jul 23 '24 15:07 goldfirere

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).

yallop avatar Jul 23 '24 21:07 yallop

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 avatar Jul 24 '24 07:07 nojb

@nojb would you want to open a PR with that implementation? Maybe we can just get this one in. Thanks for taking a stab!

goldfirere avatar Jul 29 '24 18:07 goldfirere

@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 avatar Jul 30 '24 05:07 nojb

@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.

nojb avatar Jul 30 '24 05:07 nojb

This has now been merged in the compiler. Do we close? Do we merge this? I don't know the protocol.

goldfirere avatar Oct 01 '24 14:10 goldfirere

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).

gasche avatar Oct 01 '24 19:10 gasche