# General method for new structure

**URL:** <https://discourse.julialang.org/t/general-method-for-new-structure/51666>\
**Category:** New to Julia\
**Tags:** question\
**Created:** [December 11, 2020, 2:07pm UTC](https://discourse.julialang.org/t/general-method-for-new-structure/51666 "2020-12-11T14:07:27Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![udistr](https://avatars.discourse-cdn.com/v4/letter/u/73ab20/32.png) [@udistr](https://discourse.julialang.org/u/udistr)\
**Post date:** [December 11, 2020, 2:07pm UTC](https://discourse.julialang.org/t/general-method-for-new-structure/51666/1 "2020-12-11T14:07:28Z")

</div>

Hi,

I have made the CyclicArray type ([https://github.com/udistr/CyclicArrays.jl](https://github.com/udistr/CyclicArrays.jl)) which defines a new structure:

struct CyclicArray{T,N} \<: AbstractArray{T,N}  
data::AbstractArray{T,N}  
connections  
end

Right now, I define many outer constructor methods that direct operations to the array, e.g.:

Base.size(A::CyclicArray) = size(A.data)

Is there a way to generalize this so that any function that expects one or more array arguments will perform its operation on the data array?

for example, if A is a CyclicArray type then any function f (e.g., sum, mean, size…), will be treated as:

f(A::CyclicArray) = f(A.data)

Similarly:

f(A::CyclicArray,B::CyclicArray) = f(A.data,B.data)

and so on.

Thanks,  
Udi

---

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [December 11, 2020, 2:12pm UTC](https://discourse.julialang.org/t/general-method-for-new-structure/51666/2 "2020-12-11T14:12:37Z")

</div>

Probably what you are searching for is this list of methods to implement:

[https://docs.julialang.org/en/v1/manual/interfaces/#man-interface-array](https://docs.julialang.org/en/v1/manual/interfaces/#man-interface-array)

---

<div class="post-metadata">

**Author:** ![hendri54](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hendri54/32/9621_2.png) [@hendri54](https://discourse.julialang.org/u/hendri54)\
**Post date:** [December 11, 2020, 2:41pm UTC](https://discourse.julialang.org/t/general-method-for-new-structure/51666/3 "2020-12-11T14:41:14Z")

</div>

A convenient way of delegating methods is `Lazy.jl` `@forward`. But there are other approaches:

> [@Method forwarding macro](https://discourse.julialang.org/t/method-forwarding-macro/23355):
>
> MacroTools.jl defines a [@forward macro but it’s in the examples folder](https://github.com/MikeInnes/MacroTools.jl/blob/master/src/examples/forward.jl). Lazy.jl exports [its own version of the same macro](https://github.com/MikeInnes/Lazy.jl/blob/a1b5f06ade07a442f0c009fb0bb4fe594db11fd2/src/macros.jl#L264-L288) but it’s not documented in the README. Come to think of it, while the @forward macro is quite useful, it isn’t really a macro development tool nor a functional programming tool. So I’m not too surprised that it is treated like a second-class citizen in those packages. Any thought about what kind of package would be a good fit for this?

---

<div class="post-metadata">

**Author:** ![FPGro](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fpgro/32/20822_2.png) [@FPGro](https://discourse.julialang.org/u/FPGro)\
**Post date:** [December 11, 2020, 4:00pm UTC](https://discourse.julialang.org/t/general-method-for-new-structure/51666/4 "2020-12-11T16:00:12Z")

</div>

And if you need more than just forwarding, repetitive code can easily be generated with this pattern:

[https://docs.julialang.org/en/v1/manual/metaprogramming/#Code-Generation](https://docs.julialang.org/en/v1/manual/metaprogramming/#Code-Generation)

---

<div class="post-metadata">

**Author:** ![Jean\_Michel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jean_michel/32/8282_2.png) [@Jean\_Michel](https://discourse.julialang.org/u/Jean_Michel)\
**Post date:** [December 13, 2020, 1:57pm UTC](https://discourse.julialang.org/t/general-method-for-new-structure/51666/5 "2020-12-13T13:57:02Z")

</div>

It is inconvenient to dig in these packages for the `forward` macro so I wrote my own version which just takes 10 lines and has no dependencies. This macro should be in an easier to reach place.

```julia
"""
@forward T.f f1,f2,...
  generates 
f1(a::T,args...)=f1(a.f,args...)
f2(a::T,args...)=f2(a.f,args...)
...
"""
macro forward(ex, fs)
  T, field = esc(ex.args[1]), ex.args[2].value
  fdefs=map(fs.args)do ff
      f= esc(ff)
      quote
        @inline ($f)(a::($T),args...)=($f)(a.$field,args...)
      end
  end
  Expr(:block, fdefs...)
end

```
