Ok, it should be:

macro myDef3(name)
    return esc(:( $name(x; y=3) = return (x*y) ))
end
@myDef3 f3
f3(4) # now gives 12, as expected
f3(4, y=5) #now gives 20, as expected

The return must be explicitly written. But why? I find this very confusing. 

On Monday, September 8, 2014 9:58:24 PM UTC+2, Jonas Hagen wrote:
>
> Hello!
>
> I'm trying to define a function using a macro. But I just dont get it. 
> Look at the following Examples:
>
> Using default arguments, everything works as expected:
> macro myDef2(name)
>     return esc(:( $name(x, y=3) = x*y ))
> end
> @myDef2 f2
> f2(4) # gives 12, as expected
> f2(4, 5) #gives 20, as expected
>
> So, I assumed the same would work for keyword arguments, but it doesn't:
> macro myDef3(name)
>     return esc(:( $name(x; y=3) = x*y ))
> end
> @myDef3 f3
> f3(4) # gives 3 but 12 expected
> f3(4, y=5) #gives 5 but 20 expected
>
> Even more strange:
> macro myDef4(name)
>     return esc(:( $name(x; y=3) = x )) # not using y here!
> end
> @myDef4 f4 # gives ERROR: syntax: malformed expression
>
> Am I doing something wrong or is this a bug?
> Maybe I just don't understand what esc() does or how to define functions 
> in macros, I'm new to Julia.
> I'm using Version 0.3.0 (2014-08-20 20:43 UTC).
>
> - Jonas
>

Reply via email to