从 0.6 开始,可以使用 where
在 Julia 中创建参数化方法。句法。根据release notes of 0.6版本,where
句法
can be used anywhere a type is accepted
现在考虑以下人为的例子:
function (rng::R)() where {R <: Range}
return first(rng)
end
当我尝试编译它时,会出现以下错误:
ERROR: function type in method definition is not a type
所以我的问题是在 Julia 0.6+ 中创建参数仿函数的正确方法是什么?
最佳答案
Ohkay,我基本上明白你想要做什么。了解functors
这是一个简短的示例代码。
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
这个问题其实还是OPEN
正如@gnimuc 正确指出的那样,这是一个问题。
关于julia - Julia 中的参数仿函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46063872/