I agree with you. It dispatches twice.

On Wednesday, August 26, 2015 at 6:33:43 PM UTC+2, Matt Bauman wrote:
>
> Yes, this can be surprising.  Look at `methods(u)`:
>
> julia> methods(u)
> # 5 methods for generic function "u":
> u(c::Float64) at none:7
> u(c::Float64, h::Float64) at none:3
> u(c::Float64, h::Float64, b) at none:3
> u(c::Float64, h::Float64, b, a) at none:3
> u(c::Float64, a) at none:7
>
> When you call `u(2.)`, it fills in the missing argument and then *calls 
> `u` again*, with two arguments.  Since `a` is a Float64, when it does this, 
> the method that's dispatched to isn't from the second definition (on 
> psuedo-line 7), but rather the first definition (line 3).  It's just a 
> little tricky that the convenience methods that are generated for optional 
> arguments might re-dispatch to a completely different definition.
>
> On Wednesday, August 26, 2015 at 12:22:19 PM UTC-4, Nils Gudat wrote:
>>
>> I'm defining a function with two methods as follows:
>>
>> a = 2.
>> b = 0.8
>>
>> function u(c::Float64, h::Float64, b=b, a=a)
>>     ((c/(h^b))^(1-a))/(1-a)
>> end
>>
>> function u(c::Float64, a=a)
>>      c^(1-a)/(1-a)
>> end
>>
>> However, when calling u(2.), the result is -0.87..., which should be the 
>> result of the function call u(2., 2.). 
>> Clearly I misunderstand how multiple dispatch is working here - I thought 
>> when defining a function f() with one method f(::Float64), and another 
>> method f(::Float64, ::Float64), the two-argument version would only be 
>> called if I'm actually supplying two arguments!?
>>
>

Reply via email to