Definition of function with keywoed arguments via macro

82 views
Skip to first unread message

jonas....@gmail.com

unread,
Sep 8, 2014, 3:58:24 PM9/8/14
to julia...@googlegroups.com
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

Jonas Hagen

unread,
Sep 13, 2014, 8:10:08 AM9/13/14
to julia...@googlegroups.com
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.

Tim Holy

unread,
Sep 13, 2014, 8:13:27 AM9/13/14
to julia...@googlegroups.com
You might want to file this as an issue.
https://github.com/JuliaLang/julia/issues

--Tim

Jonas Hagen

unread,
Sep 13, 2014, 8:30:15 AM9/13/14
to julia...@googlegroups.com
Thanks for the encouragement.
The issue is filed: https://github.com/JuliaLang/julia/issues/8338
Reply all
Reply to author
Forward
0 new messages