Getting type without parameters

I need to get the type version without parameters. For example, let's say I have x = [0.1,0.2,0.3] . Then typeof(x)==Array{Float64,1} . How to create a function (or does it exist?) parameterless_type(x) == Array ? I need to get this in general form in order to access the constructor without type parameters in it.

+5
source share
2 answers

It seems to work at 0.5

 julia> typeof(a) Array{Float64,1} julia> (typeof(a).name.primary)([1 2 3]) 1×3 Array{Int64,2}: 1 2 3 

Change 1:

Thanks to the thick comment and ColorTypes.jl package ColorTypes.jl solution for 0.6:

 julia> (typeof(a).name.wrapper)([1 2 3]) 1×3 Array{Int64,2}: 1 2 3 

Edit 2:

Fanyang Wang convinced me that using typename necessary. In particular, Array{Int}.name fails by 0.6 because Array{Int} now of type UnionAll . Definition working on 0.5 and 0.6,

 using Compat.TypeUtils: typename if :wrapper in fieldnames(TypeName) parameterless_type(T::Type) = typename(T).wrapper else parameterless_type(T::Type) = typename(T).primary end parameterless_type(x) = parameterless_type(typeof(x)) 

Wherein

 parameterless_type([0.1,0.2,0.3]) == Array parameterless_type(Array{Int}) == Array 
+2
source

The correct way, compatible with 0.5 and 0.6, is to use Compat .

 julia> using Compat julia> Compat.TypeUtils.typename(Array{Int, 2}) Array julia> Compat.TypeUtils.typename(Union{Int, Float64}) ERROR: typename does not apply to unions whose components have different typenames Stacktrace: [1] typename(::Union) at ./essentials.jl:119 julia> Compat.TypeUtils.typename(Union{Vector, Matrix}) Array 
+4
source

Source: https://habr.com/ru/post/1264206/


All Articles