Multiple dispatch doesn't work for named parameters?

177 views
Skip to first unread message

Daniel Carrera

unread,
Jan 30, 2016, 11:37:51 AM1/30/16
to julia-users
This is very weird. It looks like multiple dispatch doesn't work at least in some cases. Look:

julia> semimajor_axis(;M=1,P=10) = (P^2 * M)^(1/3)
semimajor_axis (generic function with 1 method)

julia> semimajor_axis(;α=25,mp=1,M=1,d=10) = α * d * M/mp / 954.9
semimajor_axis (generic function with 1 method)

julia> semimajor_axis(M=3,P=10)
ERROR: unrecognized keyword argument "P"


I do understand that it may be risky to have two functions with the same name and different named parameters (I really wish Julia allowed me to make some/all named parameters mandatory), but I was expecting that my code would work. I clearly defined a version of the function that has a named parameter called "P".

Does anyone know why this happens and what I can do to fix it? Am I basically required to either use different function names, or give up on using named parameters for everything?

Cheers,
Daniel.

Yichao Yu

unread,
Jan 30, 2016, 11:50:18 AM1/30/16
to Julia Users
This is documented[1]. Not sure if there's plan to change that.

[1] http://julia.readthedocs.org/en/latest/manual/methods/?highlight=keyword#note-on-optional-and-keyword-arguments

>
> Cheers,
> Daniel.

Andrew

unread,
Jan 30, 2016, 11:54:07 AM1/30/16
to julia-users
I see in the manual http://docs.julialang.org/en/latest/manual/methods/#note-on-optional-and-keyword-arguments that:

"Keyword arguments behave quite differently from ordinary positional arguments. In particular, they do not participate in method dispatch. Methods are dispatched based only on positional arguments, with keyword arguments processed after the matching method is identified."

I don't think you defined 2 different methods. You defined one method taking no positional arguments and a bunch of keyword arguments. Then you overwrote that method.

Daniel Carrera

unread,
Jan 30, 2016, 12:35:23 PM1/30/16
to julia-users
Oh, I see. Thanks. At least now I know.

Cheers,
Daniel.

On Saturday, 30 January 2016 17:50:18 UTC+1, Yichao Yu wrote:
This is documented[1]. Not sure if there's plan to change that. 

[1] http://julia.readthedocs.org/en/latest/manual/methods/?highlight=keyword#note-on-optional-and-keyword-arguments 


Reply all
Reply to author
Forward
0 new messages