comparison by wrapper type in julia

Viewed 80

I have a bunch of parametric singleton structs with a hierarchy, i need to stablish a comparison operation to find if a list of those singletons is "unique". where the comparison is between the main (o wrapper) types, ignoring the parametric ones, using Array and Tuple types and and the desired function myequal:

myequal(Array{Int64,1},Array{Int64,2}) #true, because both are Arrays
myequal(Array{Int64,1},Tuple{Int64,Int64}) #false

One option to define myequal is using the internal fields of the type

function myequal(a::Type,b::Type)
  ta = a.name
  tb = b.name
  return ta == tb
end

But using internal undocumented fields is not a recommended practice. Is there a canonical way to do this?

1 Answers

nameof(myType) returns a Symbol with the type name which is exactly what you need.

julia> nameof(Vector{Int})
:Array

julia> nameof(Matrix{Float64})
:Array

Now you can use those Symbols for comparison.

Related