Printing "punned" record fields

I would like to gather feedback from the community regarding the subject of record fields “punning”, i.e., for example given

type x = {a: string, b: int, c: float}

let a = "test"
let b = 1
let c = 2.

writing

let x = {a, b, c}

instead of the more verbose

let x = {a: a, b: b, c: c}

Both are valid ReScript and will be accepted by the parser, but the question is how the printer should print them (how they shall be reformatted):

  • Reason used to automatically collapse the long form into the short (“punned”) form
  • ReScript up to 9.1.4 used to expand the short (“punned”) form into the long form
  • In current master, the record will be printed like the user wrote it, in one or the other form or even mixed, e.g. the user might have written let x = {a: a, b: b, c} and the printer will reprint it that way.

My personal preference is the way Reason did it - but what about yours?

Should the printer

  • always print the short form {a, b, c}
  • always print the long form {a: a, b: b, c: c}
  • leave the input as-is

0 voters

?

The problem with the {a, b, c} format, that if we have a record with one field ReScript will consider brackets as a code block, so we’ll need to write {{a}} instead.
Not very critical, but will take some time to get used to.

I think that leave the input as-is is the worst one, because it makes programmers to think which way they want to use every time they touch a record. That might become a problem when there are people with different preferences in a team.

4 Likes

You are right, the one-element record is a special case because {a} is a code block and not a record. I didn’t mention that in the initial post to not complicate things further. The one-element record needs to be always printed as {a: a}.

4 Likes

Reason also applies punning to types, is this also being considered?

type x = string;
type t = {x};

Related discussions about punning in types can be found here: https://github.com/rescript-lang/syntax/pull/64

Thank you for your feedback! I have now closed the poll and merged https://github.com/rescript-lang/syntax/pull/534.

3 Likes