rescript-compiler icon indicating copy to clipboard operation
rescript-compiler copied to clipboard

Experiment with first-class dicts.

Open cristianoc opened this issue 2 years ago • 1 comments

Works for this:

type myDict = {name?:string, anyOtherField?: int}

let tst = (d: myDict) => switch d {
    | {name:n, something:i} => String.length(n) + i
    | {name:n} => String.length(n)
    | {something:i} => i
    | _ => 0
}

cristianoc avatar Feb 02 '24 12:02 cristianoc

Already works for creation and lookup, with no further changes, somehow, as long as type annotations : dict are added:

type myDict = {name?:string, anyOtherField?: int}

let tst1 = (d: myDict) => d.something

let d: myDict = {name: "hello", something: 5}

This also works as long as anyOtherField is declared mutable:

type myDict = {name?:string, mutable anyOtherField?: int}

let tst1 = (d: myDict) => d.something = Some(10)

cristianoc avatar Feb 08 '24 09:02 cristianoc

Closing and keeping it around under the "experiment" label as discussed.

cknitt avatar May 26 '24 18:05 cknitt