"Assignment form" function definition following `for` keyword

Hi folks. I find it weird that the following code works:

julia> begin
           for f(x)=2
               @show f(1)
               @show typeof(f)
           end
       end
f(1) = 2
typeof(f) = var"#f#1"{Int64}

I suppose the result of f(x)=2 in the second line is not iterable, so invalid iteration specification error should occurs there.

1 Like

I think it’s great that this works.

Compare

for f = (sin, cos, tan)
    @show f(1)
end

and

for x = 1
    @show x
end

I see no reason to special-case disallow the single assignment for loop just because the assignment in question is a function.

1 Like

Thanks for your reply. I also like iteration over functions.

You mean that the second line for f(x)=2 in my first post is equivalent to for f = [x->2] in the following code ?
I am confusing whether f in for f(x)=2 is function name or loop control variable.

julia> begin
           for f = [x->2]
               @show f(1)
           end
       end
f(1) = 2

Yes.