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. Thanks, Michael
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! >> >
