# Cannot \`convert\` an object of type

**URL:** <https://discourse.julialang.org/t/cannot-convert-an-object-of-type/73052>\
**Category:** General Usage\
**Created:** [December 13, 2021, 9:32pm UTC](https://discourse.julialang.org/t/cannot-convert-an-object-of-type/73052 "2021-12-13T21:32:20Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![jzr](https://avatars.discourse-cdn.com/v4/letter/j/eb9ed0/32.png) [@jzr](https://discourse.julialang.org/u/jzr)\
**Post date:** [December 13, 2021, 9:32pm UTC](https://discourse.julialang.org/t/cannot-convert-an-object-of-type/73052/1 "2021-12-13T21:32:20Z")

</div>

What is wrong with this code? Can I fix it by implementing `convert` somehow?

```julia
const Maybe{T} = Union{Some{T},Nothing}

struct Wrapper{F}
    f::F
end

struct WrappedPerson{T}
    name::Wrapper{T}
    age::Wrapper{T}
end

WrappedPerson{Maybe}(Wrapper(Some("Alice")), Wrapper(Some(25)))
# MethodError: Cannot `convert` an object of type
# Wrapper{Some{String}} to an object of type
# Wrapper{Union{Nothing, Some{T}} where T}

```

---

<div class="post-metadata">

**Author:** ![Mason](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mason/32/2423_2.png) [@Mason](https://discourse.julialang.org/u/Mason)\
**Post date:** [December 13, 2021, 9:35pm UTC](https://discourse.julialang.org/t/cannot-convert-an-object-of-type/73052/2 "2021-12-13T21:35:42Z")

</div>

[Julia’s type parameters (other than Tuple) are invariant](https://docs.julialang.org/en/v1/manual/types/).

This means that

```julia
julia> Wrapper(Some("Alice")) isa Wrapper{Maybe}
false

julia> Wrapper(Some("Alice")) isa Wrapper{<:Maybe}
true

```

---

<div class="post-metadata">

**Author:** ![jzr](https://avatars.discourse-cdn.com/v4/letter/j/eb9ed0/32.png) [@jzr](https://discourse.julialang.org/u/jzr)\
**Post date:** [December 13, 2021, 9:37pm UTC](https://discourse.julialang.org/t/cannot-convert-an-object-of-type/73052/3 "2021-12-13T21:37:06Z")

</div>

Can I fix it by defining `convert`?

---

<div class="post-metadata">

**Author:** ![Mason](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mason/32/2423_2.png) [@Mason](https://discourse.julialang.org/u/Mason)\
**Post date:** [December 13, 2021, 9:47pm UTC](https://discourse.julialang.org/t/cannot-convert-an-object-of-type/73052/4 "2021-12-13T21:47:05Z")

</div>

I think it’d be better fixed at the point where you’re making the wrappers. Rather than write

```julia
Wrapper(Some("Alice"))

```

you could write

```julia
Wrapper{Maybe}(Some("Alice"))

```

e.g.

```julia
julia> WrappedPerson{Maybe}(Wrapper{Maybe}(Some("Alice")), Wrapper{Maybe}(Some("Alice")))
WrappedPerson{Union{Nothing, Some{T}} where T}(Wrapper{Union{Nothing, Some{T}} where T}(Some("Alice")), Wrapper{Union{Nothing, Some{T}} where T}(Some("Alice")))

```

But yeah, I think you could also define some methods to `Base.convert` that turns a `Wrapper{T}` into `Wrapper{Maybe}` I guess. All of this seems pretty unidiomatic and unlikely to work well though. These are like sum types, but not well contained or bounded.
