lecture-source-jl icon indicating copy to clipboard operation
lecture-source-jl copied to clipboard

Decide on spacing for named tuples and keyword arguments

Open jlperla opened this issue 7 years ago • 2 comments

There seems to be no general consensus in julia except that

b = 5
nt = (a=2 + b,)

is a terrible idea compared to nt = (a = 2 + b,)

But is it worth always imposing the standard? That would mean

nt = (a = a, b = b, c = c) # instead of
nt = (a=a, b=b, c=c)

So one option is to say that:

  • Spaces around the = are generally preferred
  • The only times that the no-spacing around the arguments is acceptable is in the foo=bar case, i.e.
# OK
f(a=2.0, b=5.0, c=10)
# also OK
f(a = 2.0, b = 5.0, c = 10)  

# BAD
d = 5.0
f(a=2.0 + d, b=5.0, c=10)

#GOOD 
f(a = 2.0 + d, b = 5.0, c = 10)

cc: @Nosferican @arnavs

jlperla avatar Nov 13 '18 05:11 jlperla

Personally, I lean towards the spaces too. But I think that if we're putting this in the style guide, we should probably pick one or the other for the foo = bar (since isn't that most of it?)

My thought process is something like: spaces are good for delineating different objects in a logical sentence, which helps for things like naming and binding.

So that would entail

nt = @with_kw (foo = foo, bar = bar)  
return (a = a, b = b)  
model = @with_kw (a = a, 
                  b = b, 
                  c = c)

arnavs avatar Nov 13 '18 05:11 arnavs

From the Julia docs on Keyword arguments, they are making the transition in favor of having spaces apparently, On the latest stable Julia

function plot(x, y; style="solid", width=1, color="black")
    ###
end

on Julia latest dev version

function circle(center, radius; color = black, fill::Bool = true, options...)
    # draw
end

I prefer spaces between any operator.

Nosferican avatar Nov 13 '18 09:11 Nosferican