I would pick (4): sprintf should be modified to deal with it.

My reason: sprintf knows how many arguments it is expecting, so if it
sees a `...` on the last argument, it could easily emit code to
extract the elements that it needs


Also (1) / (2): splatting arguments is something that happens in a
function call at runtime. A macro is neither since it deals with the
AST (the form of the code) before it gets executed, at compile time.
Since X doesn't exist at compile time, `X...` can't be expanded yet
either. However, the sprintf macro can choose to take steps to
accommodate this syntax, or it can choose to make it an error.
Currently it's just undefined, probably because nobody has tried it
before, and then opened an issue when it didn't work.


On Mon, Apr 14, 2014 at 2:37 AM, Jeff Waller <[email protected]> wrote:
> That's pretty cool, but I have a followup.
>
> Does this mean
> 1) Writing macros is implicitly harder because they don't deal with
> (splatted) collections as easy as functions?
> 2) This is not really important because it hardly is ever done?
> 3) sprintf should be re-written as a function?
> 4) sprintf should be modified to deal with this?
> 5) sprintf can't be modified easily to deal with this and still be efficient
> (see #1-#3)
>
> On Sunday, April 13, 2014 4:31:57 PM UTC-4, John Myles White wrote:
>>
>> As far as the macro is concerned, the splat isn’t executed: it’s just
>> additional syntax that gets taken in as a whole expression.
>>
>> The contrast between how a function with splatting works and how a macro
>> with splatting works might be helpful:
>>
>> julia> function splat(a, b...)
>>        println(a)
>>        println(b)
>>        return
>>        end
>> splat (generic function with 2 methods)
>>
>> julia> splat(1, 2, 3)
>> 1
>> (2,3)
>>
>> julia> splat(1, [2, 3]...)
>> 1
>> (2,3)
>>
>> julia> macro splat(a, b...)
>>               println(a)
>>               println(b)
>>               :()
>>               end
>>
>> julia> @splat(1, 2, 3)
>> 1
>> (2,3)
>> ()
>>
>> julia> @splat(1, [2, 3]...)
>> 1
>> (:([2,3]...),)
>> ()
>>
>>
>>  — John
>>
>> On Apr 13, 2014, at 1:20 PM, Jeff Waller <[email protected]> wrote:
>>
>> > Likewise I am having problems with @sprintf
>> >
>> > Is this because @sprinf is macro?  The shorthand of expanding a printf
>> > with format the contents of an array is desirable.  I would have expected
>> > the ... operator to take an array of length 2 and turn it into 2 arguments.
>> >
>> >     julia> X=[1 2]
>> >    1x2 Array{Int64,2}:
>> >     1  2
>> >
>> >     julia> @sprintf("%d%d",1,2)
>> >     "12"
>> >
>> >     julia> @sprintf("%d%d",X...)
>> >     ERROR: @sprintf: wrong number of arguments
>> >
>> >     julia> @sprintf("%d%d",(1,2)...)
>> >     ERROR: @sprintf: wrong number of arguments
>> >
>> >     julia> @sprintf("%d",X...)
>> >     ERROR: error compiling anonymous: unsupported or misplaced
>> > expression ... in function anonymous
>> >     in sprint at io.jl:460
>> >     in sprint at io.jl:464
>> >
>> >     julia> macroexpand(quote @sprintf("%d%d",X...) end)
>> >     :($(Expr(:error, ErrorException("@sprintf: wrong number of
>> > arguments"))))
>> >
>>
>

Reply via email to