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