朱莉娅(Julia):“< ;:"是什么意思符号是什么意思? [英] Julia: what does the "<:" symbol mean?
问题描述
这在函数签名中是什么意思,例如:
convert(::Type{T}, z::Complex) where {T<:Real}
严格来说,应该区分谓词Base.:(<:)
(如@Saqib的答案所述)和<:
用来描述约束的句法用法. /p>
这种语法用法可以出现在方法的类型参数声明中,以将类型变量约束为其他类型的子类型:
f(x::T) where {T<:Real} = zero(x)
这种特殊情况是当您约束struct(struct Foo{T<:Real} ... end
)的类型参数时-约束所生成的构造函数的方法,并允许将类型构造函数仅应用于受约束的子类型.
另一方面,在类型参数之外,<:
可用于将新类型声明为某些其他(必要的是抽象)类型的子类型:
struct Foo <: Real end
尽管这两种情况都与子类型谓词的含义一致,但是您不能将它们替换为其他任意表达式(例如,您不能在f
中编写... where {isreal(T)}
).
What does this mean in function signatures, for example:
convert(::Type{T}, z::Complex) where {T<:Real}
Strictly speaking, one should differentiate between the predicate Base.:(<:)
, as described in @Saqib's answer, and the syntactic usage of <:
for describing constraints.
This syntactic usage can occur in type parameter declarations of methods, to constrain a type variable to be a subtype of some other type:
f(x::T) where {T<:Real} = zero(x)
A sort of special case of this is when you constrain the type parameter of a struct (struct Foo{T<:Real} ... end
) -- that constrains the methods of the generated constructor, and allows the type constructor to be applied only to the constrained subtypes.
On the other hand, outside of type parameters, <:
can be used to declare a new type as a subtype of some other (necessarily abstract) type:
struct Foo <: Real end
Although both cases are in line with the meaning of the subtyping predicate, you can't replace them with other arbitrary expressions (e.g., you can't write ... where {isreal(T)}
in f
).
这篇关于朱莉娅(Julia):“< ;:"是什么意思符号是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!