This solution is what I've used in these situations, but it's not really
satisfactory – it's hard to execute efficiently and it obscures issues with
incorrect keyword usage. It would be good to come up with a better way to
support this.


On Wed, Jun 18, 2014 at 5:43 PM, Leah Hanson <[email protected]> wrote:

> Using the same print_args, here's a revised arg_collector:
>
> ~~~~
> julia> function arg_collector(;kwargs...)
>
>            # do some operation on args
>
>            print_args(;kwargs...) #always print args
>        end
> arg_collector (generic function with 1 method)
>
> julia> arg_collector(x=5, y=6, z=7)
> x 5 y 6 z 7
> ~~~~
>
> You need the `...` to splice the varargs into the function call, and you
> need to put them after the `;` in the call to make the keyword arguments
> (rather than normal varargs).
>
> -- Leah
>
>
>
> On Wed, Jun 18, 2014 at 4:36 PM, Steve Kelly <[email protected]> wrote:
>
>> I have the following code that takes some keywords and does some
>> operations. This is for a wrapper for Gcode (talking to 3D printers), so
>> I'd like to specify an arbitrary axis to control. Below is what I would
>> like to do.
>>
>> function print_args(;args...)
>>>     # do some formatting
>>>     str = ""
>>>     for (key, val) in args
>>>         str *= string(key, " " , val, " ")
>>>     end
>>>     println(str)
>>> end
>>>
>>> function arg_collector(;args...)
>>>
>>>     # do some operation on args
>>>
>>
>>     print_args(args) #always print args
>>> end
>>>
>>> arg_collector(x=5, y=6, z=7)
>>>
>>
>>  The obvious work around seems to be to dispatch onto a
>> print_args(x::Array{Any,1}) and handle the formatting there. I'm not
>> inclined to do that since it creates redundant code.
>>
>> Is there a succinct way to re-pass the keyword arguments to another
>> function expecting only keyword arguments?
>>
>>
>

Reply via email to