On Mon, 2016-06-20 at 14:06, [email protected] wrote: > Hi, > > module M > function f{T1}(x::T1) return x.a end > function f{T2}(x::T2) return x.b + 1 end > export f > end
This should probably read: module M function f(x::T1) return x.a end function f(x::T2) return x.b + 1 end export f end As before it was the same method, thus the second definition overwrote the first. > using M > type T1 a::Float64 end > type T2 b::Float64 end > t1 = T1(1.0) > t2 = T2(2.0) > println(f(t1)) > println(f(t2)) > > This code doesn't work, how can I force the function templated for T1 to be > used when calling f(t1)? > Many thanks, As Fengyang said this does not work. Maybe this could be an option for you: module M abstract AT1 abstract AT2 function f(x::AT1) return x.a end function f(x::AT2) return x.b + 1 end export f, AT1, AT2 end using M type T1<:AT1 a::Float64 end type T2<:AT2 b::Float64 end t1 = T1(1.0) t2 = T2(2.0) println(f(t1)) println(f(t2))
