Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Experiment with first-class dicts. #6605

Closed
wants to merge 5 commits into from
Closed

Conversation

cristianoc
Copy link
Collaborator

@cristianoc cristianoc commented Feb 2, 2024

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
}

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
}
```
With lbl_all mutable, it can be extended when new fields are used in pattern matching.

This handles examples with multiple fields:

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

let tst = (d: myDict) => switch d {
    | {a:i, b:j} => i + j
    | _ => 0
}

```
@cristianoc
Copy link
Collaborator Author

cristianoc commented Feb 8, 2024

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 cristianoc changed the title Draft dict pattern matching. Experiment fir first-class dicts. Feb 8, 2024
@cristianoc cristianoc changed the title Experiment fir first-class dicts. Experiment with first-class dicts. Feb 8, 2024
@cknitt
Copy link
Member

cknitt commented May 26, 2024

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

@cknitt cknitt closed this May 26, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants