Forward all methods of a structure

You code do this with some code generation. As a simple example

for op ∈ ops_i_want_to_use
    @eval $op(m::Daggered, args...) = $op(m.block, args...)
end 

though ideally you shouldn’t use splatting. In practice I often see people do this separately for unary, binary and trinary operators, but another (probably better) alternative would be to use MacroTools.jl which provides some really useful tools for deconstructing function syntax.

Note also that “Daggered” already exists, it’s called Adjoint and you can get it by just doing A' or adjoint(A) for some matrix A. (Be sure to import the package Compat first, as I’m showing syntax for 0.7 and I think adjoint was called something else in 0.6.)

One further comment, I’m not sure if your example was just demonstrative or intended as an actual use, but in general, if you want to create an array type, it should inherit from AbstractArray. For example

struct Daggered{T,N} <: AbstractArray{T,N}
    block::Array{T,N}
end

this will then inherit all of the methods of AbstractArray (which is a lot). You only need to implement a few simple functions to make it work. One of the great virtues of Julia is that inheriting from abstract types in Base is a much more common practice than in any other language I have seen, and arrays are a really good example of how powerful this can be.

1 Like