Julia 中的代表团是什么? [英] What is delegation in julia?

查看:18
本文介绍了Julia 中的代表团是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

I see occational references to delegation, or the delegation design pattern in Julia.

What is this?

E.g. I see it mentioned in

解决方案

This is a form of polymorphism via composition (rather than inheritance)

Say one has a wrapper type, wrapping some instance of a concrete subtype of AbstractT where the wrapper itself is intended to be a subtype of AbstractT (Not nesc always true, but in general).

To add all the methods one exacts such a subtype of AbstractT to have, we want to delegate some or all of those methods to the wrapped object. We do this via metaprogramming. There are a few varients on how to do this. But in general it is a hard pattern to abstract so people often write their own.

Say that that all AbstractT subtypes should implement 1arg length, size and mean

struct WrappedT{T<:AbstractT} <: AbstractT
    backing ::T
    ...
end

for fun in (:(Base.length), :(Base.size), :(Statistics.mean))
    @eval ($fun)(x::WrappedT, args...) = ($fun(x.backing, args...))
end

Generally you won't delegate all the methods, since some you will want to do differently, that is the point of creating the new type after all.

这篇关于Julia 中的代表团是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆