`k` is iterable and generates pairs of (keyword, value). So the second
line constructs a Dict mapping key names to values. There's an idea
floating around (issue #4916) to make `k` already a dictionary of some
kind without this extra step.

Depending on the rest of your code, you might not need "k...".
Something like `f(method; two=0, three=0)` would work just as well in
your example, and you wouldn't need the Dict.

On Sun, Feb 23, 2014 at 12:07 AM, Joshua French <[email protected]> wrote:
> Hello everyone,
>
> I am trying to create a function where they keyword arguments (essentially)
> depend on some other initial argument.  I was able to cobble some code
> together that works, but I'm not sure if there is a better way.
> Additionally, I have no idea what the second line is doing.  The basic idea
> of this code is that if method == "a", then I need to use use the argument
> with keyword "two".  Alternatively, if method == "b", then I need to use the
> argument with keyword "three".
>
> My two questions are:
> 1.  What is the second line of the function actually doing?
> 2.  Is there a better way of doing this?
>
> Self-contained code is below.
>
> Thanks, Joshua.
>
> function f(method; k...)
>     k = { i[1] => i[2] for i in k }
>     if(method == "a")
>         print(k[:two])
>     elseif(method == "b")
>         print(k[:three])
>     end
> end
>
> f("a", two = reshape(1:10, 5, 2), three = reshape(1:8, 4, 2))
> f("b", two = reshape(1:10, 5, 2), three = reshape(1:8, 4, 2))
>

Reply via email to