Record fields punning

Is not record field punning possible(try link)?

type count = {num: int}

let num = 4
let countexample = {{num}}

Punning with one attribute has ambiguities with a new { } scope.

you need to do {num: num} for single attribute records.

In contrast, if you have more than one attribute, this works perfectly fine.

type count = {num: int, num2: int}

let num = 4
let num2 = 4
let countexample = {
  {num, num2}
}

Playground Link

4 Likes

I’ve noticed that:

let countexample = {num, num2}

gets reformatted to:

let countexample = {num: num, num2: num2}

Is this deliberate? I like how field punning makes code concise.

Apparently, it was overlooked:

3 Likes

Seems to work with a trailing comma for single field records:

let countexample = {
  { num, }
}

Playground link

And using parens also work but it is pretty much equivalent to the scope version, looks a bit better in a single line:

let countexample = ({ num, })

Playground link 2

If you click “format” the punning gets replaced with { num: num }

Yes it is a bug, see the comment above Record fields punning - #4 by fham

It’s not a bug currently, but it is inconsistent. We’ll see what we can do.

2 Likes