Exotic iteration syntax

I’ve never seen this iteration syntax before. It seems that any function application can be used as a variable of iteration, so long as its arguments (but not necessarily the function itself) are defined, and the function “declaration” is syntactically valid. Where is this documented? Why is it supported? and when would one want to use it?

x = "hi"
# "hi"
[x for f(x) in 1:3]
# ["hi", "hi", "hi"]
[f(x) for f(x) in 1:3]
# [1, 2, 3]
[f(y) for f(y) in 1:3]
# ERROR: UndefVarError: y not defined
[f(0) for f(0) in 1:3]
# ERROR: syntax: "0" is not a valid function argument name
[f(x, x) for f(x, x) in 1:3]
# ERROR: syntax: function argument name not unique: "x"
1 Like

In bed on phone, but what happens if

x = 3
[f(x) for f(x) in (sin(x^2), cos(x^2), tan(x^2))]

Half not expecting that to work, but it could be imagined. More likely explanation is just that f(x) = 3 is a valid variable (and function) assignment, so it happily loops through.

[f(x) for f(y) in 1:3]

Is probably less opaque.

I like this explanation. Perhaps the [a for b in [d,e,f]] expands to something similar to

b = d
b = e
b = f

so if b := f(x), that still makes sense.