A question about parametric types

Hi
I have the following problem:

struct MyType{T}
end

function get_root_type(typ::Type)
#what goes here?
end

rt = get_root_type(MyType{Int})

i need rt to contain type MyType , without any parameter.

Is there a way to do this?
Thanks

How about this?

struct MyType{T}
end

get_root_type{T}(::MyType{T}) = T

get_root_type(typ::Type) = get_root_type(eval(:($typ()))

Then

julia> rt = get_root_type(MyType{Int})
Int64

This is not what i want, i don’t want the parameter, i want MyType
and MyType can be any type with parameters.
Thanks.

Oh okay, this is one way of getting it using Regex

get_root_type(typ::Type) = match(r"[^({.+})]+",string(typ)).match |> Symbol |> eval

Then

julia> rt = get_root_type(MyType{Int})
MyType
get_root_type(typ::Type) = typ.name.wrapper
1 Like

That will not change anything.

julia> struct MyType{T}
       end

julia> typeof(MyType)
UnionAll

julia> UnionAll <: Type
true

What’s your intended application?

1 Like

In v0.7, there is nameof, eg

julia> struct MyType{T} end

julia> nameof(MyType{Int})
:MyType

In v0.6 I think it was Base.datatype_name.

This is gloriously horrible :wink:

2 Likes

this is Julia, so if you don’t know an official way of doing things, you can usually at least easily create an inefficient way of doing something, until someone corrects it

As a general rule, going to strings and back for reflection/metaprogramming is usually a sign that you are not using something provided by the language.

Yes you are right, this calls for an application related solution which i generated the required functions via metaprogramming.

Thanks…

Thanks.
This also works.

This wouldn’t work if the type is in a different module such as MyModule.MyType
Thanks

Try

function get_root_type(typ::Type)
    typ.name
end

Purists may object, but it gets the job done.