Julia中的参数函子
从0.6开始,可以使用Julia中的参数函子,julia,functor,Julia,Functor,从0.6开始,可以使用where语法在Julia中创建参数化方法。根据版本,其中语法 可以在接受类型的任何位置使用 现在考虑下面的设计实例: function (rng::R)() where {R <: Range} return first(rng) end 所以我的问题是在Julia 0.6+中创建参数函子的正确方法是什么 e.q.julia>相同类型(x::T,y::T),其中{T}=true 例如,julia>函数(p::多项式)(x)。。。结束 据我所知,没有“类
where
语法在Julia中创建参数化方法。根据版本,其中
语法
可以在接受类型的任何位置使用
现在考虑下面的设计实例:
function (rng::R)() where {R <: Range}
return first(rng)
end
所以我的问题是在Julia 0.6+中创建参数函子的正确方法是什么
- e.q.
julia>相同类型(x::T,y::T),其中{T}=true
- 例如,
julia>函数(p::多项式)(x)。。。结束
Julia> function (rng::Range)()
return first(rng)
end
cannot add methods to an abstract type
当前的docu没有提到函数类对象对具体类型的任何限制,但不幸的是Julia无论如何都不接受 哦,我基本上明白你想做什么了。要理解
函子
,这里有一个简短的示例代码
julia> struct Student
name::String
end
julia> function (::Student)()
println("Callable of Student Type!")
end
julia> object = Student("JuliaLang")
Student("JuliaLang")
julia> object()
Callable of Student Type!
但是当我尝试创建参数化函子时,它抛出了与您类似的错误
julia> function (::T)() where {T <: Student}
println("Callable of Student Type!")
end
ERROR: function type in method definition is not a type
julia>函数(::T)()其中{T相关:这是我没有混淆任何东西,问题正是公式化的。你将参数化方法与函数类对象混合在一起。没有相关的文档。请阅读where
语法的0.6发行说明。它说“…可以在接受类型的任何地方使用”很明显,发行说明不精确或错误,isa(1,T),其中{T这就是为什么我要澄清它。这是问题的全部要点。但是没有任何混淆,问题仍然是这样表述的!
julia> function (::T)() where {T <: Student}
println("Callable of Student Type!")
end
ERROR: function type in method definition is not a type