julia - Julia 中的参数仿函数

标签 julia functor

从 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/

相关文章:

pointers - 如何使用BLAS函数传递指向子数组的指针?

haskell - 为什么 Functor 不公开 fmap 的默认实现?

haskell - 在什么情况下可以/不可以拥有数据类型的 Functor 实例?

julia - 如何在 Julia 中删除数组数组中的数组

syntax-error - 我在 Julia 中的程序发现没有语法错误

c++ - 在模板化类和函数 :-) 的上下文中,具有默认值的仿函数作为函数中的参数

c++ - 在一个函数中嵌入不同的函数 : functor or lambda?

c++ - 既然可以使用函数引用,为什么还要使用仿函数

julia - 在 Julia - Frozen 中添加包

arrays - Julia 缺少数组构造函数?