Sergey
Sergey

Reputation: 1328

julia incorrect type when subtyping parametric supertype

I have an abstract type

abstract type PointND{N<:Integer, T<:Number} end

When I subtype it, setting N=1

struct Point1D{T} <: PointND{1, T}
    x::T
end

Julia throws error

ERROR: TypeError: in PointND, in N, expected N<:Integer, got Int64

even though (typeof(1)<:Integer) == true.

This happens even when N is of type Number. Why doesn't this work?

Upvotes: 4

Views: 169

Answers (1)

Simeon Schaub
Simeon Schaub

Reputation: 775

Your N here is not a subtype of Integer, but an instance of a subtype of Integer. If you actually try 1 <: Integer in the REPL, it will throw an error. So PointND will only accept types that subtype Integer, like Int or UInt, but not instances of subtypes of Integer like 1 or 0xff. The solution here is actually just not to restrict the type of N at all, and document clearly instead that N should always be an integer. There isn't currently any way to restrict the type of instances of bit types in type parameters.

Upvotes: 6

Related Questions