Formatting assignment in function call

I assign to a variable in the condition of a while loop in this working code:

getdata() = rand() < 0.5 ? rand() : missing

function dowork()
    while !ismissing((val = getdata();))

When I format the file with JuliaFormatter.format_file(), the semicolon is removed. The code then errors with “ERROR: MethodError: no method matching ismissing(; val::Float64)”.

Should this syntax not be allowed or is this a bug in JuliaFormatter?

The versions are Julia 1.9.3, JuliaFormatter 1.0.36.

I suspect this is a bug in the formatter or the parser that it uses. You could use this synatax instead:

while (val = getdata(); !ismissing(val))
    # body

However, I’d argue that just putting a check and break in the middle of a while true loop is clearer:

while true
    val = getdata()
    ismissing(val) && break
    # body

Thanks Stefan, I’ve submitted an issue with JuliaFormatter.

1 Like