Types 在Julia的方法中传播类型参数

Types 在Julia的方法中传播类型参数,types,julia,Types,Julia,Julia(v1.0)在使用类型别名时未在方法定义之外传播自由类型参数: const RT{R<:Real} = Type{R} rt(::RT{R}) where R = R rt2(::Type{R}) where {R<:Real} = R # there's more logic here for other subtypes 但是rt()可以自由地接受非方法(rt2) #1通用函数“rt2”的方法: [1] rt2(::键入{R}),其中R可以写入(编辑): 现在呢

Julia(
v1.0
)在使用类型别名时未在方法定义之外传播自由类型参数:

const RT{R<:Real} = Type{R}

rt(::RT{R}) where R = R
rt2(::Type{R}) where {R<:Real} = R
# there's more logic here for other subtypes 
但是
rt()
可以自由地接受非
方法(rt2)
#1通用函数“rt2”的方法:
[1] rt2(::键入{R}),其中R可以写入(编辑):

现在呢

julia> methods(rt)
# 1 method for generic function "rt":
[1] rt(r::Type{R} where R<:Real) in Main at REPL[16]:1

julia> rt(Int)
Int64
julia>方法(rt)
#1通用函数“rt”的方法:
[1] rt(r::Type{r},其中r rt(Int)
Int64

所有的工作都如预期的那样。它看起来像方法定义中的
where
子句,如果存在,会覆盖
const
中的约束。我不确定这是否是有意的。

rt(Int)
导致
错误:UndervarError:R未定义
。此外,方法签名显示
rt(::Type{R}好的。我已经修好了。这是一个范围问题。
julia> rt(Char)
Char

julia> rt2(Char)    
MethodError: no method matching rt2(::Type{Char})
julia> methods(rt)
# 1 method for generic function "rt":
[1] rt(::Type{R}) where R in Main at REPL[2]:1

julia> methods(rt2)
# 1 method for generic function "rt2":
[1] rt2(::Type{R}) where R<:Real in Main at REPL[3]:1
rt(r::RT) = r
julia> methods(rt)
# 1 method for generic function "rt":
[1] rt(r::Type{R} where R<:Real) in Main at REPL[16]:1

julia> rt(Int)
Int64