繁体   English   中英

julia 子类型化参数超类型时类型不正确

[英]julia incorrect type when subtyping parametric supertype

我有一个抽象类型

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

当我输入它时,设置N=1

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

Julia 抛出错误

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

即使(typeof(1)<:Integer) == true

即使NNumber类型,也会发生这种情况。 为什么这不起作用?

您的N不是Integer的子类型,而是Integer子类型的实例。 如果你真的在 REPL 中尝试1 <: Integer ,它会抛出一个错误。 所以 PointND 将只接受Integer子类型的类型,如IntUInt ,但不接受Integer子类型的实例,如10xff 这里的解决方案实际上根本不限制N的类型,而是清楚地记录N应该始终是 integer。 目前没有任何方法可以限制类型参数中位类型实例的类型。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM