Proposal: Allow variable assignments inside loop headers

Occasionally, I want to write loops like the following:

foo = 2
while foo^=2 < 32
    println(foo)
end

In julia 0.6.2, this throws the following error:

syntax: invalid identifier name "^="

Assignments return the assigned object and it would be great if we could take use of this behavior in loops (or other control flow expressions).

What do others think?

Does this do what you’d like? The error message seems unclear to me, but the crux of the problem is precedence:

julia> foo = 2
2

julia> while (foo ^= 2) < 32
           @show foo
end
foo = 4
foo = 16
6 Likes

Whoops, yeah, silly mistake :slight_smile:
Thanks @yurivish!

1 Like