Why don’t different non-optional keyword parameters specify multiple dispatches

julia> foo(a; b, d="d", e="e") = (@show a b d e)
foo (generic function with 1 method)

julia> foo(a; m, d="d", e="e") = (@show a m d e)
foo (generic function with 1 method)

julia> foo(a; b, c, d="d", e="e") = (@show a b c d e)
foo (generic function with 1 method)

Is there a particular reason why this doesn’t work? I assume if the keyword argument does not have a default value, there shouldn’t be any ambiguity to choose which method to call?

Thank you!

Keyword parameters do not define dispatch (note that you have only one method there). You can however define default values for optional parameters:

https://docs.julialang.org/en/v1/manual/functions/#Optional-Arguments

1 Like

Sorry, I think maybe I haven’t conveyed my question clearly enough. My question is more like “why don’t different keyword parameters specify multiple dispatches”.

If they are optional parameters I can understand because we would encounter cases where the number and the types of the required (position) arguments are the same, which creates ambiguity for which method to call.

However, if those keyword arguments are not optional parameters, that means apart form passing additional variable names to pair with the arguments’ value, they “should” be no different from (position) arguments.

As for (position) arguments, we don’t need to define a specific type of each argument to form multiple dispatches, just the number of arguments is enough:

julia> g(a) = nothing
g (generic function with 1 method)

julia> g(a, b) = nothing
g (generic function with 2 methods)

I would like to know the language design philosophy behind this decision.

I cannot help with the explanation of why exactly is that. But you can of course use this pattern:

f(a;b,c=1) = f(a,b,c)
f(a,b,c) = ...

To create and use methods associated with those optional or not keyword parameters.

1 Like

Cool! Thanks!

1 Like