# Run-time Dynamic type-casting (RTTI)

**URL:** <https://discourse.julialang.org/t/run-time-dynamic-type-casting-rtti/64852>\
**Category:** Internals & Design\
**Tags:** question\
**Created:** [July 18, 2021, 10:55am UTC](https://discourse.julialang.org/t/run-time-dynamic-type-casting-rtti/64852 "2021-07-18T10:55:33Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![JerryM](https://avatars.discourse-cdn.com/v4/letter/j/e47c2d/32.png) [@JerryM](https://discourse.julialang.org/u/JerryM)\
**Post date:** [July 18, 2021, 10:55am UTC](https://discourse.julialang.org/t/run-time-dynamic-type-casting-rtti/64852/1 "2021-07-18T10:55:33Z")

</div>

Hello,  
in ANSI C/C++, C#, Rust etc. there is MISSING this type of dynamic casting:

```julia
struct MyStruct { int item; };
MyStruct ms;
char dynTypeName[] = "MyStruct";
void *ptr;
ptr = &ms;
dynamic_cast<dynTypeName>(ptr)->item = 1; 

```

Ok, it would be very useful, but missing. And My question is:

Is there an equivalent of such dynamic run-time type-casting in Julia ?

Jerry

---

<div class="post-metadata">

**Author:** ![anon56330260](https://avatars.discourse-cdn.com/v4/letter/a/f07891/32.png) [@anon56330260](https://discourse.julialang.org/u/anon56330260)\
**Post date:** [July 18, 2021, 11:35am UTC](https://discourse.julialang.org/t/run-time-dynamic-type-casting-rtti/64852/2 "2021-07-18T11:35:31Z")

</div>

I guess this is meaningless in Julia, since Julia doesn’t have pointers (in normal use cases). You can directly use abstract types and cast it down to concrete type at runtime, just like in those dynamic languages. Example:

```julia
function f(x::Vector{Integer})
    for i in x
        if isa(i,Int)
            cast_i = i::Int
            println("is a Int")
        elseif isa(i,UInt8)
            cast_i = i::UInt8
            println("is a UInt8")
        end
    end
end

```

---

<div class="post-metadata">

**Author:** ![JerryM](https://avatars.discourse-cdn.com/v4/letter/j/e47c2d/32.png) [@JerryM](https://discourse.julialang.org/u/JerryM)\
**Post date:** [July 18, 2021, 11:49am UTC](https://discourse.julialang.org/t/run-time-dynamic-type-casting-rtti/64852/3 "2021-07-18T11:49:46Z")

</div>

> [@anon56330260](#):
>
> `x::Vector{Integer}`

ok, your aswer means, that the ::Vector can contain different types of variables. e.g.

x[0] is Int  
x[1] is UInt8  
x[2] is double  
etc…

ok, thanks for reply …

---

<div class="post-metadata">

**Author:** ![Sukera](https://avatars.discourse-cdn.com/v4/letter/s/ce7236/32.png) [@Sukera](https://discourse.julialang.org/u/Sukera)\
**Post date:** [July 18, 2021, 1:34pm UTC](https://discourse.julialang.org/t/run-time-dynamic-type-casting-rtti/64852/4 "2021-07-18T13:34:39Z")

</div>

It can only contain objects whose type is a subtype of `Integer`, so a `double` (`Float64` in julia) would not be allowed.

---

<div class="post-metadata">

**Author:** ![JerryM](https://avatars.discourse-cdn.com/v4/letter/j/e47c2d/32.png) [@JerryM](https://discourse.julialang.org/u/JerryM)\
**Post date:** [July 18, 2021, 1:44pm UTC](https://discourse.julialang.org/t/run-time-dynamic-type-casting-rtti/64852/5 "2021-07-18T13:44:36Z")

</div>

> [@Sukera](#):
>
> a `double` ( `Float64` in julia) would not be allowed.

“…a `double` ( `Float64` in julia) would not be allowed…” ??? it is terrible …  
I am very disappointed. ☹

---

<div class="post-metadata">

**Author:** ![anon56330260](https://avatars.discourse-cdn.com/v4/letter/a/f07891/32.png) [@anon56330260](https://discourse.julialang.org/u/anon56330260)\
**Post date:** [July 18, 2021, 2:27pm UTC](https://discourse.julialang.org/t/run-time-dynamic-type-casting-rtti/64852/6 "2021-07-18T14:27:53Z")

</div>

Then you can use `Vector{Number}` or `Vector{Any}`. `Any` is the supertype of all the types, and `Number` is the supertype of all the number types (including double, int and complex). If you just want real number type, you can consider `Real` type.  
P.S. It seems that you are unclear about Julia’s type system. You can read the [Types](https://docs.julialang.org/en/v1/manual/types/) section to get more info. Julia is quite different to those static-type languages like C#, C++ and go.

---

<div class="post-metadata">

**Author:** ![Sukera](https://avatars.discourse-cdn.com/v4/letter/s/ce7236/32.png) [@Sukera](https://discourse.julialang.org/u/Sukera)\
**Post date:** [July 18, 2021, 4:38pm UTC](https://discourse.julialang.org/t/run-time-dynamic-type-casting-rtti/64852/7 "2021-07-18T16:38:44Z")

</div>

I’m not sure I understand why you’re disappointed? Surely you don’t expect floating point numbers to behave exactly like integers…? They’re very different things after all, both conceptually as well as how they’re represented in the computer. If you want a container that can hold a mixture of both while keeping their values, as has been said above, `Vector{Real}` or `Vector{Any}` are possibilities (or even `Vector{Union{Integer, Float64}}`, which can hold double precision floating points as well as any integer type like Int8, Int32 etc. but no other subtype of `Real`).

Additionally, you may not be aware that `Integer` is not the same as `Int` - the former is an abstract type in the type hierarchy, while the latter is a concrete type (a leaf that can’t be subtyped) in the type tree.

---

<div class="post-metadata">

**Author:** ![JerryM](https://avatars.discourse-cdn.com/v4/letter/j/e47c2d/32.png) [@JerryM](https://discourse.julialang.org/u/JerryM)\
**Post date:** [July 18, 2021, 5:11pm UTC](https://discourse.julialang.org/t/run-time-dynamic-type-casting-rtti/64852/9 "2021-07-18T17:11:30Z")

</div>

ok, thanks.
