# Converting abstract types

**URL:** <https://discourse.julialang.org/t/converting-abstract-types/79763>\
**Category:** General Usage\
**Tags:** question, parametric-types\
**Created:** [April 21, 2022, 4:50am UTC](https://discourse.julialang.org/t/converting-abstract-types/79763 "2022-04-21T04:50:51Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![tictaccat](https://avatars.discourse-cdn.com/v4/letter/t/9f8e36/32.png) [@tictaccat](https://discourse.julialang.org/u/tictaccat)\
**Post date:** [April 21, 2022, 4:50am UTC](https://discourse.julialang.org/t/converting-abstract-types/79763/1 "2022-04-21T04:50:51Z")

</div>

Suppose I have an abstract type `A{V}` and two structs `B{V}` and `C{V}`, for example

```julia
abstract type A{V} end

struct B{V} <: A{V}
   x::V
end

struct C{V} <: A{V}
   x::V
end

```

Now suppose I have a function `f` that accepts `A{V}`. In other words, what is coming in could either be `B{V}` or `C{V}`. I want to construct an instance of the appropriate concrete type where I have switched out `V` for an `Int`. If I do it for each concrete type, that’s super easy. Here’s an example:

```julia
f(x::B{V}) where {V} = B{Int}(3)
f(x::C{V}) where {V} = C{Int}(3)

```

But I would like to the above generically. For all I know, there could be tons more subtypes of `A{V}` out there. I would like to write a function `f(x::A{V})` that somehow figures out what the hidden `B` or `C` is and swaps out the `V` for an `Int`.

I know that in general we cannot assume anything about how the subtype of `A{V}` is parametrized. But it still makes sense to want to get the “appropriate” type based on the input type that is a subtype of `A{Int}`, irrespective of / ignoring `V`. My question is: how can I write a function `f(x::A{V})` that returns something that is a subtype of `A{Int}` but has the same “concrete form” as what came in? If this requires writing a specialized method (e.g. some sort of `convert` rule) for each concrete subtype of `A`, I’m fine with that, I would just like to know what the solution is

---

<div class="post-metadata">

**Author:** ![tictaccat](https://avatars.discourse-cdn.com/v4/letter/t/9f8e36/32.png) [@tictaccat](https://discourse.julialang.org/u/tictaccat)\
**Post date:** [April 21, 2022, 5:13am UTC](https://discourse.julialang.org/t/converting-abstract-types/79763/3 "2022-04-21T05:13:25Z")

</div>

Ah! It seems like what I want to do is overload the `similar` function for each of the concrete types, as stated here [Stripping parameter from parametric types - #10 by jameson](https://discourse.julialang.org/t/stripping-parameter-from-parametric-types/8293/10)

Marking this as the solution for now, although please correct me if I’m wrong:)
