> OK, so that is the long way to do it.
> I just thought that, since multiple dispatch does not work on keyword
> arguments, they should by definition be the same for all methods, no?
> Otherwise you end up with keyword arguments throwing an error because the
> type of a different argument changing, an error that is hard to debug
> because the kwargs do not show up under a methods(foo) call. Seems to me
> such a potentially ripe source of bugs that julia would have an inbuilt (or
> at least recommended ideomatic) way of dealing with it.

No, I don't think that would help either as keywords are not necessarily
the same for different methods.  What you can do is:

f(x,y; kws...) = (deal_with_kws(kws); ...)

Or use metaprogramming to make sure all you keywords are the same.

You should consider using an option structure.  I think Parameters.jl
can help with that:

@with_kw immutable Opts
   t=5
   u=9
end

f(x,y, opts=Opts()) = ...
f(x, opts=Opts()) = ...
...

# call with default opts
f(x,y)
# call with non-default opts
f(x,y, Opts(t=7))

> Den onsdag den 6. januar 2016 kl. 00.09.10 UTC+1 skrev Jeffrey Sarnoff:
>>
>> If you want all methods to use the same keyword arguments, define each
>> method with the same keyword arguments.  You can declare keyword arguments'
>> types.
>>
>> function foo(a::Int; test::Bool = true)
>>    if test
>>        a
>>    else
>>        0
>>    end
>> end
>>
>> function foo(a::AbstractFloat; test::Bool = true)
>>    if test
>>        a
>>    else
>>        100.0
>>    end
>> end
>>
>> foo(1)
>> 1
>> foo(1,false)
>> 0
>> foo(1,"grue")
>> ERROR
>>
>> foo(1.0)
>> 1.0
>> foo(1.0,false)
>> 100.0
>> foo(1.0,"grue")
>> ERROR
>>
>>
>>
>> On Tuesday, January 5, 2016 at 4:30:14 PM UTC-5, Michael Borregaard wrote:
>>>
>>> Hi, sorry I have a simple question, I have tried to RTFM.
>>>
>>> If I have a function with multiple methods and keyword arguments, all
>>> methods should share the same keyword args, right? How do I write this?
>>>
>>> function foo(a::Int; test = true)
>>> 2+a
>>> end
>>>
>>> function foo(a::AbstractFloat #how do I continue this line?
>>>
>>> In my real-world example I have multiple methods and a long list of
>>> keyword arguments.
>>>
>>> Thanks!
>>>
>>

Reply via email to