BlueStyle icon indicating copy to clipboard operation
BlueStyle copied to clipboard

Relax opinion on use of `;` in NamedTuples?

Open nickrobinson251 opened this issue 5 years ago • 3 comments

Current guidnace is "NamedTuples should not be prefixed with ; at the start" (related to https://github.com/invenia/BlueStyle/pull/22)

As the current examples show this leads to these three cases having different style:

xy = (x=1, y=2)
x1 = (x=1,)  # Trailing comma required for correctness.
xs = (; kwargs...)  # Semicolon required to splat correctly.

I can't help but feel that this is a simpler rule:

xy = (; x=1, y=2)
x1 = (; x=1,) 
xs = (; kwargs...) 

Also, I write a lot of code in codebases that follow BlueStyle and I don't recall anyone ever corrected me on adding/removing a ;, so i wonder how much this guidance is even followed right now.

(related to #7, https://github.com/domluna/JuliaFormatter.jl/issues/283#issuecomment-687815553)

nickrobinson251 avatar Sep 06 '20 15:09 nickrobinson251

Existing guidance

# Yes:
xy = (x=1, y=2)
x = (x=1,)  # Trailing comma required for correctness.
x = (; kwargs...)  # Semicolon required to splat correctly.

# No:
xy = (x = 1, y = 2)
xy = (;x=1,y=2)
x = (; x=1)

Suggested change to

# Yes:
xy = (; x=1, y=2)
x = (; x=1,)  
x = (; kwargs...) 

# Ok:
xy = (x=1, y=2)

# No:
xy = (x = 1, y = 2)
xy = (;x=1,y=2)

nickrobinson251 avatar Sep 09 '20 22:09 nickrobinson251

My preference is to not use the semi-colon. You are correct in that the rule is more straight forward with always including the semi-colon but the only case it's required is for splatting so really we're just enforcing the use of two extra characters to gain a little extra consistency. Most people probably don't encounter (; kwargs...) which is why I'm good with leaving things as they are.

omus avatar Sep 10 '20 13:09 omus

IMO the no-semicolon version is somewhat unsafe due to how easy it is to forget a trailing comma on a single-element NamedTuple. This has been a pretty persistent and difficult-to-resolve source of bugs in our code base. My opinion is that leading semicolons in NamedTuples should at least be in the Ok section, if not the Yes.

jonniediegelman avatar Apr 14 '24 23:04 jonniediegelman