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

Returning updated records #509

Closed
endeav0r opened this issue Apr 9, 2018 · 1 comment
Closed

Returning updated records #509

endeav0r opened this issue Apr 9, 2018 · 1 comment

Comments

@endeav0r
Copy link
Contributor

endeav0r commented Apr 9, 2018

The following code illustrates the bug (This will run in http://gluon-lang.org/try/)

let {List} = import! "std/list.glu"

type R = { a: String, b: Int }

let f r n : R -> Int -> R =
  {b=n, ..r}

let r = {a="hello", b=1}

let r2 = f r 2

r2.b

The order of members of R has been changed, so that where we had {a: String, b: Int} we now have {b: Int, a: String}, and these two types are, apparently, not equal.

<top>:Line: 6, Column: 3: Expected the following types to be equal
Expected: <top>.R
Found: { b : Int, a : String }
2 errors were found during unification:
Field names in record do not match.
	Expected: a
	Found: b
Field names in record do not match.
	Expected: b
	Found: a
  {b=n, ..r}
  ^~~~~~~~~~

This makes it difficult to create functions which update and return specific fields in complex structs/records.

@Marwes
Copy link
Member

Marwes commented Apr 9, 2018

I think this case may be solvable by taking the type of ..r into account first, before appending and replacing the other fields. That way field order of r is preserved making the behavior less surprising.

More generally we might want to accept code like this.

type R = { a: String, b: Int }

let r: R = {b=1, a="hello"}
r

where the field order is "wrongly" specified. I think the only way to solve that is to have a deterministic field order for records (can be done by sorting the fields lexically).

Marwes added a commit to Marwes/gluon that referenced this issue Sep 23, 2018
bors bot added a commit that referenced this issue Sep 23, 2018
638: fix: Use the field order of the base record in record updates r=Marwes a=Marwes

Fixes #509

Co-authored-by: Markus Westerlind <[email protected]>
@bors bors bot closed this as completed in #638 Sep 23, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants