# Auto promote or convert

**URL:** <https://discourse.julialang.org/t/auto-promote-or-convert/7350>\
**Category:** General Usage\
**Created:** [November 28, 2017, 7:43am UTC](https://discourse.julialang.org/t/auto-promote-or-convert/7350 "2017-11-28T07:43:56Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Qiyamah](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/qiyamah/32/2660_2.png) [@Qiyamah](https://discourse.julialang.org/u/Qiyamah)\
**Post date:** [November 28, 2017, 7:43am UTC](https://discourse.julialang.org/t/auto-promote-or-convert/7350/1 "2017-11-28T07:43:56Z")

</div>

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

```nohighlight
julia> type DD
           x::Int
       end

julia> type EE
           x::Int
       end
julia> function f(a::EE)
           println(a.x)
       end
f (generic function with 1 method)

julia> t = DD(5)
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?  
Thanks

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [November 28, 2017, 7:53am UTC](https://discourse.julialang.org/t/auto-promote-or-convert/7350/2 "2017-11-28T07:53:04Z")

</div>

Strictly speaking, fully “automatic” promotion it is not possible because of the design of the language:  
[https://docs.julialang.org/en/latest/manual/conversion-and-promotion/](https://docs.julialang.org/en/latest/manual/conversion-and-promotion/)

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

---

<div class="post-metadata">

**Author:** ![fredrikekre](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fredrikekre/32/1688_2.png) [@fredrikekre](https://discourse.julialang.org/u/fredrikekre)\
**Post date:** [November 28, 2017, 7:54am UTC](https://discourse.julialang.org/t/auto-promote-or-convert/7350/3 "2017-11-28T07:54:26Z")

</div>

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))`.
