# Getting the base type in 0.6

**URL:** <https://discourse.julialang.org/t/getting-the-base-type-in-0-6/2425>\
**Category:** General Usage\
**Created:** [March 3, 2017, 1:32pm UTC](https://discourse.julialang.org/t/getting-the-base-type-in-0-6/2425 "2017-03-03T13:32:54Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![cstjean](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cstjean/32/1444_2.png) [@cstjean](https://discourse.julialang.org/u/cstjean)\
**Post date:** [March 3, 2017, 1:32pm UTC](https://discourse.julialang.org/t/getting-the-base-type-in-0-6/2425/1 "2017-03-03T13:32:55Z")

</div>

Given `t = SomeType{Int64}`, we used to be able to get `SomeType` via `t.name.primary`. That no longer works in 0.6. How should I do that? `t.name` still exists, but it’s a `TypeName`.

Alternatively, all I really want is, given an object of type `SomeType{Int64}`, to create a similar object of the same “base type”, but with a different instantiation of the parameter, eg. `SomeType{Float64}`.

---

<div class="post-metadata">

**Author:** ![cstjean](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cstjean/32/1444_2.png) [@cstjean](https://discourse.julialang.org/u/cstjean)\
**Post date:** [March 3, 2017, 1:47pm UTC](https://discourse.julialang.org/t/getting-the-base-type-in-0-6/2425/2 "2017-03-03T13:47:11Z")

</div>

Also, what’s the “official name” for `Nullable`’s relationship to `Nullable{Int64}`? Primary type?

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [March 3, 2017, 2:58pm UTC](https://discourse.julialang.org/t/getting-the-base-type-in-0-6/2425/3 "2017-03-03T14:58:53Z")

</div>

> [@cstjean](#):
>
> Alternatively, all I really want is, given an object of type `SomeType{Int64}`, to create a similar object of the same “base type”, but with a different instantiation of the parameter, eg. `SomeType{Float64}`.

The right way to do this is usually to write a function that does the conversion you want, e.g.:

```julia
Base.float{T}(x::SomeType{T}) = SomeType{float(T}}(x)

```

Define a method for each `SomeType` you want to convert.

(For example, the `similar` function does this for array types.)

---

<div class="post-metadata">

**Author:** ![PabloZubieta](https://avatars.discourse-cdn.com/v4/letter/p/ee7513/32.png) [@PabloZubieta](https://discourse.julialang.org/u/PabloZubieta)\
**Post date:** [March 3, 2017, 3:00pm UTC](https://discourse.julialang.org/t/getting-the-base-type-in-0-6/2425/4 "2017-03-03T15:00:23Z")

</div>

You are not supposed to extract that to create a new type like that (see some discussion of this [here](https://github.com/JuliaLang/julia/pull/20006#discussion_r96127963)).

You said you want a similar object of the same base type, well, the word _similar_ should give you a hint

```julia
struct SomeType{T}
    field::T
end

# two possible definitions, but it would depend exactly on what you want
similar(::T, s::SomeType) where T<:SomeType = T(s.field)
similar(s::SomeType, T::Type) = SomeType{T}(s.field)

```

---

<div class="post-metadata">

**Author:** ![jameson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jameson/32/23_2.png) [@jameson](https://discourse.julialang.org/u/jameson)\
**Post date:** [March 8, 2017, 7:30pm UTC](https://discourse.julialang.org/t/getting-the-base-type-in-0-6/2425/5 "2017-03-08T19:30:47Z")

</div>

> [@cstjean](#):
>
> Nullable’s relationship to Nullable{Int64}?

A supertype or a typename (note however that the former is ambiguous and the latter is not a type).

---

<div class="post-metadata">

**Author:** ![cstjean](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cstjean/32/1444_2.png) [@cstjean](https://discourse.julialang.org/u/cstjean)\
**Post date:** [March 8, 2017, 8:57pm UTC](https://discourse.julialang.org/t/getting-the-base-type-in-0-6/2425/6 "2017-03-08T20:57:04Z")

</div>

My use case is that I manipulate mathematical expressions such as `Multiply(Variable(:x), 5)`, and my variable substitution algorithm might replace `Variable(:x)` with a number. If `Multiply` is parametric, I cannot use `typeof(expr)(map(y->subst(y, substitutions), fieldsof(expr))...)`, because `typeof(expr)` is `Multiply{Variable, Int}`. It was convenient to have a generic way of traversing the tree and changing subtrees, without defining N trivial methods for my N function/predicate types, but I guess it’s no big deal.

---

<div class="post-metadata">

**Author:** ![fengyang.wang](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fengyang.wang/32/104_2.png) [@fengyang.wang](https://discourse.julialang.org/u/fengyang.wang)\
**Post date:** [March 9, 2017, 4:12pm UTC](https://discourse.julialang.org/t/getting-the-base-type-in-0-6/2425/7 "2017-03-09T16:12:28Z")

</div>

I don’t recommend traversing the tree, but the way to do it on 0.6 is `Compat.TypeUtils.typename`, and then possibly a `.primary` field access.
