Is there a way to make a Set of Types that all inherit from a supertype?

For instance a Set that contains Integer types like UInt8, Int64, and so on. Or a Set that contains Exception types.

First instinct would be

Set{Type{Exception}}()

But that can only store the type Exception

You can use

Set{DataType}()

But that just lets any type into the Set.

Is there a way to make a Set that can only contain children of some superset?

Hi! It seems that what you need is Set{Type{<:Exception}}:

julia> e = Set{Type{<:Exception}}()
Set{Type{<:Exception}}()

julia> push!(e, ErrorException, ArgumentError)
Set{Type{<:Exception}} with 2 elements:
  ErrorException
  ArgumentError

julia> push!(e, Int)
ERROR: MethodError: Cannot `convert` an object of type 
  Type{Int64} to an object of type
  Type{<:Exception}
[...]
1 Like