# Forward all methods of a structure

**URL:** <https://discourse.julialang.org/t/forward-all-methods-of-a-structure/11783>\
**Category:** New to Julia\
**Created:** [June 19, 2018, 1:42pm UTC](https://discourse.julialang.org/t/forward-all-methods-of-a-structure/11783 "2018-06-19T13:42:33Z")\
**Posts on this page:** 1\
**Showing post:** 2

<div class="post-metadata">

**Author:** ![ExpandingMan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/expandingman/32/866_2.png) [@ExpandingMan](https://discourse.julialang.org/u/ExpandingMan)\
**Post date:** [June 19, 2018, 1:56pm UTC](https://discourse.julialang.org/t/forward-all-methods-of-a-structure/11783/2 "2018-06-19T13:56:51Z")

</div>

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

```julia
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](https://github.com/MikeInnes/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

```julia
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](https://docs.julialang.org/en/latest/manual/interfaces/#man-interface-array-1) 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.

---

_[View the full topic](https://discourse.julialang.org/t/forward-all-methods-of-a-structure/11783)._
