Is it bad style to write `a = foo()` instead of `a, _ = foo()`?

Suppose I have a function that returns two values, but I am usually only interested in the first one.

function foo()
    nit = 0
    res = 1.
    while res > 0.1 && nit < 500
        nit += 1
        res /= 1 + res
    return res, nit

The typical way to signal to someone reading my code that I am not going to use the nit value would be to write

R, _ = foo()

However, I figured out that in Julia I can simply write

R = foo()

to the same effect.

Is this poor style?

julia> function foo()
           return 1,3
foo (generic function with 1 method)

julia> R = foo();

julia> length(R)

julia> R
(1, 3)

not the same?


Indeed it is not. Turns out I had renamed my function along the way and R = foo() worked by referencing the old name.

A similar issue is whether this is bad style:

julia> x, = 1,2
(1, 2)

julia> x

For me the answer is yes, but really it’s bad that Julia allows that at all.


I agree. Here’s an issue tracking it: Stricter tuple destructuring. Error on `(a, b) = (1, 2, 3)` · Issue #37132 · JuliaLang/julia · GitHub

1 Like