A most harrowing collection of Julia WATs

Okay here’s a good one from this thread a bit related to a WAT documented in the OP blog and noticed by the same @jakobnissen! I’ll condense it here:

julia> @which sum(i for i in 1:3; init=0)
(::Base.var"#sum##kw")(::Any, ::typeof(sum), a) in Base at reduce.jl:532

julia> @which sum(i for i in 1:3, init=0)
sum(a; kw...) in Base at reduce.jl:532

julia> @which sum((i for i in 1:3), init=0)
(::Base.var"#sum##kw")(::Any, ::typeof(sum), a) in Base at reduce.jl:532

In the middle case, the iterator is not (i for i in 1:3), it’s (i for i in 1:3, init in 0) because in is lowered to = (we use that symbol for so many things), and the typo is silent because it’s valid to iterate over many scalars. Always remember to parenthesize your Generator comprehensions if they’re not the only argument in a call!

9 Likes