[ANN] JuliaFormatter.jl

I don’t think so; ;s in function calls are only relevant when splatting kwargs, afaict.

In Julia, keyword argument is not optional (i.e. you can’t switch the order of positional argument by giving it a name), so f(a=0) is equivalent to f(; a=0) because as soon as you wrote a=, it’s a keyword argument, no matter if you explicitly put ; before it or not

1 Like

Thanks. And sorry, my question was not formulated right.
In the following snipped, how can I avoid the insertion of the semicolon?

julia> format_text("DataFrame(mi = rand(3))",YASStyle())
"DataFrame(; mi=rand(3))"

why* do you want to avoid it? it’s according to the code style you picked that all keyword arguments are preceded by ;. Again, I want to stress that it absolutely shouldn’t break your code because they are equivalent

1 Like

What about when you have something like foo(a=10; b = 20). is a=10 different from b=20 ?

you can’t, because you can’t optionally add names to positional argument in Julia

julia> f(a; b) = a+b
f (generic function with 1 method)

julia> f(1;b=3)
4

julia> f(a=1;b=3)
ERROR: MethodError: no method matching f(; a=1, b=3)

this is incorrect:

1 Like

I could swear this was a thing before, maybe it changed in 1.6

Sorry for derailing this a little bit but ; will be inserted before arguments for all code styles. It’s more explicit which is why it’s favored, even if there are no non-positional arguments.

Ok. So I gather there is no argument that would let me control this.

Below is my MWE (ok I guess it is a WE) where the formatting breaks my code.

using JuliaFormatter
using Dash
using DashCoreComponents
using DashHtmlComponents
using DashTable

fileorig = joinpath(mktempdir(),"afile.jl")
txtorig = """html_div(style=Dict("width"=>"300px"),[dcc_dropdown(id = "input-id0", options = [Dict(("label" => "some text"), ("value" => "AA")), Dict("label" => "something else"), ("value" => "pdc")], value="AA",multi = false)])"""
open(fileorig,"w") do f
    write(f,txtorig)
end

include(fileorig) #works

format_file(fileorig,YASStyle())

include(fileorig) #fails

No, it was not a thing. Positional arguments have not been named since at least 0.7.

2 Likes

Huh, I did not realize that it’s allowed to mix positional and kwargs like that. JuliaFormatter should probably format f(x = 2, 33, y = 4) to f(33; x = 2, y = 4).

1 Like

The latter part of this sentence is correct while the former is not.

This code works:

julia> foo(a=10; b=20) = a, b
foo (generic function with 2 methods)

julia> foo(5)
(5, 20)

@jling is correct that you cannot call positional arguments by name but you can in fact assign them a default value, e.g.

# `a` is a positional argument with default value 10
julia> f(a=10) = a
f (generic function with 2 methods)

julia> f()
10

julia> f(5)
5

# `a` is a keyword argument with default value 10
julia> f(;a=10) = a * 2
f (generic function with 2 methods)

julia> f(a=5)
10

Thanks. I did not realize this was the cause here. I filed an issue with JuliaFormatter.

I assumed that’s the calling of a function, not definition. In definition, = means default value, of course you can have f(a=10; b=10) otherwise how do you give default value to positional argument

2 Likes

Gotcha! My misunderstanding, carry on :grimacing:

1 Like

This is what I was thinking of and in fact by moving the semicolon before the positional argument with the default value you lose the ability to give that argument a value without explitcitly using the keyword.

BlueStyle and YASStyle opt to make this explicit by default

julia> s0
"f(x = 2; y = 4)"

julia> format_text(s0, DefaultStyle())
"f(x = 2; y = 4)"

julia> format_text(s0, BlueStyle())
"f(; x=2, y=4)"

julia> format_text(s0, YASStyle())
"f(; x=2, y=4)"

This is not an option right now it but it could be

please stop confusing calling function from defining function, you’re calling the funtionc f(), what you thought about before (that makes a difference where ; is), was function definition:

julia> s0 = "f(x = 2; y = 4) = 3"
"f(x = 2; y = 4) = 3"

julia> format_text(s0, DefaultStyle())
"f(x = 2; y = 4) = 3"

julia> format_text(s0, BlueStyle())
"f(x=2; y=4) = 3"

julia> format_text(s0, YASStyle())
"f(x=2; y=4) = 3"

I never though we’re talking about function definition because

was function calling

2 Likes

Apologies. My bad. I initially started with another example (where a leading ; is inserted).
But at least that was a minimal example :slight_smile:

1 Like

@jling thanks for clearing this up. I think knowing this difference we can make a couple improvements to the formatter.

1 Like

AFAIK formatter doesn’t have a bug and handles it correctly (as can be seen above in my example after turning them into definition)

1 Like