If you add println statements at the beginning of the macro then you'll see
the quoted arguments more clearly:

julia> @horner .75 c
> x: 0.75
> p: (:c,)


(as Jameson said)

julia> @horner .75 c...
> x: 0.75
> p: (:(c...),)                      <---------------------- key point here:
> this quoted expression is passed to the macro context
> ERROR: unsupported or misplaced expression ... <---------------- you get
> this error when the macro is *evaluated*


Why?

julia> macroexpand( :( @horner .75 c...) )  <---- note that I am quoting
> the macro here
> x: 0.75
> p: (:(c...),)   <--- macro context
>

and this is what is actually compiled:


> quote  <---- this is the resulting AST
>     #129#t = 0.75
>     c...
> end


Which makes no sense:

julia> c...
> ERROR: unsupported or misplaced expression ...


Compare to:

julia> macroexpand( :(@horner .75 1 2 3 4 5) )
> x: 0.75
> p: (1,2,3,4,5)
> quote
>     #130#t = 0.75
>     1 + #130#t * (2 + #130#t * (3 + #130#t * (4 + #130#t * 5)))
> end


You can (mostly) ignore the #130# stuff. Those are temporary variables for
macro hygiene to avoid conflicting with existing variable names when the
macro is pasted in to another AST.

(I didn't have any Lisp/Scheme experience when starting with Julia, and the
dualism here definitely took me a while to get used to)



On Sat, Aug 30, 2014 at 12:02 AM, Don MacMillen <don.macmil...@gmail.com>
wrote:

> Oops typo in last responce, I meant.
>
>
> julia> @horner(.75, c...)
> ERROR: unsupported or misplaced expression ...
>
> julia>
>
> On Friday, August 29, 2014 9:00:10 PM UTC-7, Don MacMillen wrote:
>>
>> Ah, well that was perhaps a bad example.  My understanding was the
>> you could interpolate an interable.  Consider then the horner macro
>>
>> macro horner(x, p...)
>>            ex = esc(p[end])
>>            for i = length(p)-1:-1:1
>>                ex = :($(esc(p[i])) + t * $ex)
>>            end
>>            Expr(:block, :(t = $(esc(x))), ex)
>>        end
>>
>> if I have a vector of constant coefficients, shouldn't I be able to splice
>> them into the macro call?
>>
>> julia> c = [1.:5]
>> 5-element Array{Float64,1}:
>>  1.0
>>  2.0
>>  3.0
>>  4.0
>>  5.0
>>
>> julia> @horner(.x, c...)
>> ERROR: syntax: invalid identifier name "."
>>
>>
>>
>> On Friday, August 29, 2014 8:45:00 PM UTC-7, Jameson wrote:
>>>
>>> that would be utterly pointless, since you can already just write:
>>> @mymacro(“aaa”, “bbb”, “ccc”)
>>>
>>> if you are intending to look at values, you should be using a function.
>>> a macro is a function but it's also special in that it takes the quoted AST
>>> of it’s arguments during parsing, not their values during runtime
>>>
>>> observe when b... is getting printed:
>>>
>>> julia> macro mymacro(a,b)
>>>        println(b)
>>>        end
>>>
>>> julia> f() = @mymacro(a,b...)
>>> b...
>>> f (generic function with 1 method)
>>>
>>> julia> f()
>>>
>>> ​
>>>
>>>
>>> On Fri, Aug 29, 2014 at 9:45 PM, Don MacMillen <don.ma...@gmail.com>
>>> wrote:
>>>
>>>> I meant something like this
>>>>
>>>> julia> macro mymacro(a,b,c)
>>>>        println(c)
>>>>        end
>>>>
>>>> julia> @mymacro("aaa", ("bbb", "ccc")...)
>>>> ERROR: wrong number of arguments
>>>>
>>>> which works fine for functions
>>>>
>>>> julia> function myfunc(a,b,c)
>>>>        println(c)
>>>>        end
>>>> myfunc (generic function with 1 method)
>>>>
>>>> julia> myfunc("aaa", ("bbb", "ccc")...)
>>>> ccc
>>>>
>>>>
>>>>
>>>>
>>>> On Friday, August 29, 2014 6:02:45 PM UTC-7, Jameson wrote:
>>>>
>>>>> splicing into a macro works for me:
>>>>>
>>>>> julia> macro mymacro(a,b)
>>>>>        println(b)
>>>>>        end
>>>>>
>>>>> julia> @mymacro(x, y...)
>>>>> y...
>>>>>
>>>>> ​
>>>>>
>>>>>
>>>>> On Fri, Aug 29, 2014 at 8:57 PM, Don MacMillen <don.ma...@gmail.com>
>>>>> wrote:
>>>>>
>>>>>> The slides are great.  Many thanks for sharing.
>>>>>>
>>>>>> I do have a question about macros that maybe you can answer.  In your
>>>>>> nb on
>>>>>> metaprogramming you have the horner macro listed and it uses a
>>>>>> temporary
>>>>>> variable t.  But this macro can be written without using a temporary
>>>>>> variable.
>>>>>> It turns out to be slower (the no temp version) if we are computing a
>>>>>> bunch of
>>>>>> polynomials with the same coefficients, but is a tiny bit faster if
>>>>>> the coefficients
>>>>>> are always changing. So are the Expr's cached? Or is something else
>>>>>> going on?
>>>>>>
>>>>>> Also (OK I have two questions) it looks like we cannot splice into a
>>>>>> macro call?
>>>>>> Ie  @mymacro(x, y...) doesn't work?
>>>>>>
>>>>>> Thanks again.
>>>>>>
>>>>>> Don
>>>>>>
>>>>>>
>>>>>> On Friday, August 29, 2014 4:08:44 AM UTC-7, Steven G. Johnson wrote:
>>>>>>>
>>>>>>> I just gave a talk on Julia at EuroSciPy, and managed to escape
>>>>>>> alive.  :-)
>>>>>>>
>>>>>>> I think they will post a video at some point, but in the meantime
>>>>>>> the slides and IJulia notebooks are posted at:
>>>>>>>
>>>>>>>        https://github.com/stevengj/Julia-EuroSciPy14
>>>>>>>
>>>>>>> --SGJ
>>>>>>>
>>>>>>
>>>>>
>>>

Reply via email to