How can I define a function with a given name?
let fname = :myfunction
@eval quote
function $(fname)()
42
end
end
end
UndefVarError: fname not defined
How can I define a function with a given name?
let fname = :myfunction
@eval quote
function $(fname)()
42
end
end
end
UndefVarError: fname not defined
@eval operates in global scope. That’s why it can’t see fname.
EDIT: This is the wrong answer. See below.
IMHO, it’s no coincidence that @eval and @evil sound so similar ![]()
Did you mean to write
@eval begin
function $(fname)()
42
end
end
instead of
@eval quote
function $(fname)()
42
end
end
? The former should work just fine.