# How to refactor code which is using the fallback constructor

**URL:** https://discourse.julialang.org/t/how-to-refactor-code-which-is-using-the-fallback-constructor/10841
**Category:** General Usage
**Created:** [May 11, 2018, 12:53pm UTC](https://discourse.julialang.org/t/how-to-refactor-code-which-is-using-the-fallback-constructor/10841 "2018-05-11T12:53:08Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![essenciary](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/essenciary/32/210469_2.png) [@essenciary](https://discourse.julialang.org/u/essenciary)
#### Post date: [May 11, 2018, 12:53pm UTC](https://discourse.julialang.org/t/how-to-refactor-code-which-is-using-the-fallback-constructor/10841/1 "2018-05-11T12:53:08Z")

</div>

Per the v0.7 what’s new:

> The fallback constructor that calls convert is deprecated. Instead, new types should prefer to define constructors, and add convert methods that call those constructors only as necessary

What’s the recommended way to refactor code which relies on this behavior. Take this simple example - how should it be rewritten?

```julia
import Base.convert

struct Continent
  name::String
end

struct Country
  name::String 
  continents::Vector{Continent}
end

convert(Continent, x::String) = Continent(x)

c = Country("Turkey", ["Europe", "Asia"])

```

Thanks!

---

<div class="post-metadata">

### Author: ![tshort](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tshort/32/43_2.png) [@tshort](https://discourse.julialang.org/u/tshort)
#### Post date: [May 11, 2018, 1:13pm UTC](https://discourse.julialang.org/t/how-to-refactor-code-which-is-using-the-fallback-constructor/10841/2 "2018-05-11T13:13:56Z")

</div>

Here’s one way to define your own fallback:

```julia
Country(name, continents) = Country(string(name), [Continent(x) for x in continents])

```

---

<div class="post-metadata">

### Author: ![essenciary](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/essenciary/32/210469_2.png) [@essenciary](https://discourse.julialang.org/u/essenciary)
#### Post date: [May 11, 2018, 1:20pm UTC](https://discourse.julialang.org/t/how-to-refactor-code-which-is-using-the-fallback-constructor/10841/3 "2018-05-11T13:20:29Z")

</div>

Thank you

OK, so the idea is to define a “very” generic external constructor, which takes `Any` types of arguments where it makes sense – and explicitly convert these to the types expected by the default constructor.
