julia - 在 Julia 中调用父仿函数

标签 julia dispatch

自 1.3 版起,Julia 允许 functor dispatch on abstract types 。因此,我想知道是否可以从子对象显式调用父仿函数。

例如在下面的示例中,有什么方法可以通过 bar::Bar 对象调用 (x::Foo)() 吗?

abstract type Foo end
(x::Foo)() = "Invoked Foo functor."

struct Bar <: Foo end
(x::Bar)() = "Invoked Bar functor."

bar = Bar()
@info "Calling bar functor directly."
bar() |> println
@info "Invoking parent functor."
# ??? - how to invoke parent functor (x::Foo)() (e.g. desired output "Invoked Foo functor")
invoke(bar,Tuple{Bar}, bar) |> println

最佳答案

使用默认结构怎么样?

abstract type Foo end
(x::Foo)() = "Invoked Foo functor."

struct Bar <: Foo end
(x::Bar)() = "Invoked Bar functor."

struct DefaultFoo <: Foo end
#here we dont define an specialized method, DefaultFoo calls Foo

#an interface to define default methods: 
default(x::Type{Foo}) = DefaultFoo


function parent(x::T) where {T}
     y = default(supertype(T))
     return y()
end

最后,您可以这样做来调用默认函数:

bar = Bar()
foo = parent(bar)
foo()

这需要为每个父类(super class)型定义一个 defaultFoo 类型和一个 default(x::Type{T}) 。您可以使用以下宏自动执行此操作:

macro create_default_functor(type)
    a = gensym(type)
    esc(quote
        struct $a <: $type end
        default(x::Type{$type})  = $a
    end)
end

使用宏和您的代码:

abstract type Foo end
(x::Foo)() = "Invoked Foo functor."
@create_default_functor Foo
struct Bar <: Foo end
(x::Bar)() = "Invoked Bar functor."

function parent(x::T) where {T}
     y = default(supertype(T))
     return y()
end

#calling bar
bar = Bar()
bar()
#calling foo
foo = parent(bar)
foo()

我现在不具备直接在抽象类型定义上调用宏的宏知识,但这是一个开始。 关于抽象仿函数的事情是,这是一个非常新的功能(1.3 尚未推出),也许可以在 Julia 的 future 版本中添加此功能(例如 call_parent(Foo,args...), )如果您添加建议该功能的 PR。

关于julia - 在 Julia 中调用父仿函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58565177/

相关文章:

arrays - 删除长度为 1 的 Julia 数组维度

ios - Dispatch.main.async 节中的行数无效

angular - 在 ngrx effect angular 中做某事之前等待两个序列 Action

google-app-engine - Google App Engine 中的 dispatch.yaml 增加了响应时间

matplotlib - 如何使用 Julia 的 PyPlot.jl 向图形添加补丁

julia - 在 Julia 中估计概率分布并从中采样

julia - 在 Julia : ArgumentError: provide a valid sink argument, 中导入 CSV 文件,如 `using DataFrames; CSV.read(source, DataFrame)`

node.js - "dispatch()"是什么意思/做什么,为什么当我们有 .then() 和 .catch() 时使用它

function - Julia:多种类型的相同功能?

julia - 在 Julia 中使用 ndgrid/meshgrid 功能