In 0.4 you can do this using callable types, which is a convenient way of 
encapsulating parameters. That would let you use the syntax u(2.) .

Alternatively, why not just drop the default parameters and pass them 
explicitly? The way I did this recently was to make use of the type system 
and multiple dispatch

immutable CRRA
    a::Float64
end
immutable CRRA_hours
    a::Float64
    b::Float64
end
function u(UF::CRRA, c)
    a = UF.a
    c^(1-a)/(1-a)
end
function u(UF::CRRA_hours, c, h)
    a = UF.a
    b = UF.b
    ((c/(h^b))^(1-a))/(1-a)
end
a = 2.
b = 0.8
UF = CRRA(a)
u(UF, 2.)

outputs -0.5 and

UF = CRRA_hours(a, b)
u(UF, 2., 0.5)

outputs -0.2871745887492587.


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