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

Relax opinion on use of ; in NamedTuples? #62

Open
nickrobinson251 opened this issue Sep 6, 2020 · 3 comments
Open

Relax opinion on use of ; in NamedTuples? #62

nickrobinson251 opened this issue Sep 6, 2020 · 3 comments

Comments

@nickrobinson251
Copy link
Contributor

nickrobinson251 commented Sep 6, 2020

Current guidnace is "NamedTuples should not be prefixed with ; at the start" (related to #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, domluna/JuliaFormatter.jl#283 (comment))

@nickrobinson251
Copy link
Contributor Author

nickrobinson251 commented Sep 9, 2020

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)

@omus
Copy link
Contributor

omus commented Sep 10, 2020

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.

@jonniediegelman
Copy link

jonniediegelman commented Apr 14, 2024

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants