Auto promote or convert


Hi guys,
I am looking for a way to do the following:

julia> type DD

julia> type EE
julia> function f(a::EE)
f (generic function with 1 method)

julia> t = DD(5)

julia> f(t)
ERROR: MethodError: no method matching f(::DD)
Closest candidates are:
  f(::EE) at REPL[4]:2

I want t which is type of DD to be converted to type EE version automatically . What should i do for that?


Strictly speaking, fully “automatic” promotion it is not possible because of the design of the language:

You should write a method for f that takes DD if you want to use this syntax.


You can not do that, since you define your function only for ::EE it will only accept that. You can either just drop ::EE and the function will work for both DD and EE since they have the same field name x, or just define a constructor EE(a::DD) = EE(a.x) and call f like f(EE(t)).