Explicit "return" in anonymous functions

question

#1

I’m not sure whether it’s a bug or a feature, so posting it here.

The following constructs generate identical code (tested with both v0.5.2 and v0.6.0):

julia> map(x -> x, [1,2,3])
3-element Array{Int64,1}:
 1
 2
 3

julia> map(x -> begin return x end, [1,2,3])
3-element Array{Int64,1}:
 1
 2
 3

I thought that the next one should also be identical, but it’s not:

julia> map(x -> return x, [1,2,3])
ERROR: MethodError: no method matching (::##9#10)()
Closest candidates are:
  #9(::Any) at REPL[5]:1
Stacktrace:
 [1] map(::##9#10) at ./abstractarray.jl:1930
 [2] macro expansion at ./REPL.jl:97 [inlined]
 [3] (::Base.REPL.##1#2{Base.REPL.REPLBackend})() at ./event.jl:73

It seems here “return” refers to [some] outer block.
Is it by design or it’s a bug? Is it discussed somewhere in the manual?


#2

Looks to me like it’s getting parsed as map(x -> return (x, [1, 2, 3]) )

e = :(map(x -> return x, [1,2,3]))
e.args[2].args[2].args[2].args[1]

#3

Ah, indeed! And this one works:

julia> map(x -> (return x), [1,2,3])
3-element Array{Int64,1}:
 1
 2
 3