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

Need to be able to default destructured values that are renamed. #6532

Open
B-Teague opened this issue Feb 22, 2024 · 2 comments · May be fixed by #6640
Open

Need to be able to default destructured values that are renamed. #6532

B-Teague opened this issue Feb 22, 2024 · 2 comments · May be fixed by #6640

Comments

@B-Teague
Copy link
Contributor

B-Teague commented Feb 22, 2024

I came across a scenario where I needed to destructure multiple instances of a record: point1 and point2 that had an optional z coordinate for 3 dimensions. Please see example below

Point a : { x: Frac a, y: Frac a, z ? Frac a }

distanceBetweenPoints : Point a, Point a -> Frac a
distanceBetweenPoints = \point1, point2 ->
    { x: x1, y: y1, z: z1 ? 0.0 } = point1
    { x: x2, y: y2, z: z2 ? 0.0 } = point2
    Num.sqrt (
            Num.pow (x2 - x1) 2.0 +
            Num.pow (y2 - y1) 2.0 +
            Num.pow (z2 - z1) 2.0
    )
── RECORD PARSE PROBLEM in main.roc ────────────────────────────────────────────

I am partway through parsing a record, but I got stuck here:

14│      {x:x1, y:y1, z: z1 ? 0.0} = p1
@roboteng
Copy link
Contributor

I was curious and looked into this a bit. It looks like the line { x: x1, y: y1, z ? 0.0 } = point1 can compile, but the line { x: x1, y: y1, z: z2 ? 0.0 } = point1 doesn't compile. It seems the root cause that you can't combine the optional record field with the renaming, both of which are needed here.

This was referenced Apr 7, 2024
@roboteng
Copy link
Contributor

Until this is solved, I found this workaround:

distanceBetweenPoints : Point a, Point a -> Frac a
distanceBetweenPoints = \point1, point2 ->
    { x: x1, y: y1, z: z1 } =
        { x, y, z ? 0.0 } = point1
        { x, y, z }

    { x: x2, y: y2, z: z2 } =
        { x, y, z ? 0.0 } = point2
        { x, y, z }

    Num.sqrt
        (
            Num.pow (x2 - x1) 2.0 +
            Num.pow (y2 - y1) 2.0 +
            Num.pow (z2 - z1) 2.0
        )

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants