Skip to content
This repository was archived by the owner on Jun 15, 2023. It is now read-only.

Printer: auto-pun record fields #534

Merged
merged 2 commits into from
Jun 16, 2022
Merged

Conversation

cknitt
Copy link
Member

@cknitt cknitt commented Jun 10, 2022

In #480, I already prevented the printer from expanding {a, b} to {a: a, b: b}.

At the time, I refrained from making it collapse {a: a, b: b} to {a, b} like it used to be the case in Reason.
But I think it would actually be the better behavior to always print record fields as punned when possible.

Now that the next release is going to be a major version (10.0), it would IMHO be fine to make that change.

@IwanKaramazow
Copy link
Contributor

But I think it would actually be the better behavior to always print record fields as punned when possible.

What's the reasoning behind this? I'm in general not against this PR, I'm just wondering what your thought process is.

@cristianoc
Copy link
Contributor

What's the corresponding behaviour for types? Being consistent there could be positive, all else equal.

@cknitt
Copy link
Member Author

cknitt commented Jun 11, 2022

@cristianoc @IwanKaramazow Some history: Reason used to do this for both record values and types, i.e.

type a = int;
type b = string;

type c = { a: a, b: b };

let a = 1;
let b = "";

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

would reformat to

type a = int;
type b = string;

type c = {
  a,
  b,
};

let a = 1;
let b = "";

let c = {a, b};

ReScript (up to the last release 9.1.4) used to do the inverse - change {a, b} to {a: a, b: b} for both values and types. I always felt this was a regression, as the shorter form seems much more readable to me. Much less "visual noise" IMHO, especially for records with many fields and/or long field names.

Current state in ReScript after #480 is that record values are printed exactly the way the user wrote them (whether punned or not), but record types are still expanded. The latter is because Cheng and Bob stated in #64 (comment) and #64 (comment) that they didn't want punning for record type declarations.

I do not care that much about the type declarations, but I strongly favor the punned version for record values. It is nice that, since #480, the printer is leaving the punned version untouched, but I would prefer if the punned version was actually enforced by the printer to ensure a consistent and more readable style in ReScript codebases.

@cristianoc
Copy link
Contributor

It seems for each combination one could come up with some argument. Can't see compelling reasons either way.

@cristianoc
Copy link
Contributor

Wondering how relevant the question for types is. Most often the names would be different.

@cristianoc
Copy link
Contributor

@cknitt any thoughts on how to progress this? Perhaps more feedback from the forum?

@cknitt
Copy link
Member Author

cknitt commented Jun 13, 2022

Yes, will ask on the forum later today. 👍

@IwanKaramazow
Copy link
Contributor

One thought is that for the type counterparts, punning might be pretty confusing:

type coordinate = {x, y: int}

One could argue that some people will interpret x as having type int and not x.

@cristianoc
Copy link
Contributor

One thought is that for the type counterparts, punning might be pretty confusing:

type coordinate = {x, y: int}

One could argue that some people will interpret x as having type int and not x.

Remember that discussion. Yes symmetry between types and values does not seem like a good idea.

@cristianoc
Copy link
Contributor

Looks like this can be merged, based on discussions in the forum.

@cristianoc
Copy link
Contributor

Leaving a link to the discussion: https://forum.rescript-lang.org/t/printing-punned-record-fields/3447

@cknitt
Copy link
Member Author

cknitt commented Jun 16, 2022

I have added test cases for single-element records (which cannot be punned).

I will now close the discussion and merge the PR.

@cknitt cknitt merged commit cd86626 into rescript-lang:master Jun 16, 2022
@cknitt cknitt deleted the auto-punning branch June 16, 2022 05:53
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants