# Developing Pass-Through Types

**URL:** <https://discourse.julialang.org/t/developing-pass-through-types/10717>\
**Category:** General Usage\
**Created:** [May 4, 2018, 10:14pm UTC](https://discourse.julialang.org/t/developing-pass-through-types/10717 "2018-05-04T22:14:46Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![ccoffrin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ccoffrin/32/400_2.png) [@ccoffrin](https://discourse.julialang.org/u/ccoffrin)\
**Post date:** [May 4, 2018, 10:14pm UTC](https://discourse.julialang.org/t/developing-pass-through-types/10717/1 "2018-05-04T22:14:47Z")

</div>

A few times I have run into this situation, I would like to make a new type that only has one field, for example,

```julia
struct Foo{T}
    bar::Vector{T}
end

```

The general case is that I would like instances of Foo to behave just like instances of Vector and I would like all of the methods that work on Vector to work on Foo (simply by using the value stored in bar). However, in a rare occasion, I would like to dispatch on the type Foo differently than on the type Vector.

Is there a Julia pattern for getting this sort of behavior?

---

<div class="post-metadata">

**Author:** ![ScottPJones](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/scottpjones/32/146_2.png) [@ScottPJones](https://discourse.julialang.org/u/ScottPJones)\
**Post date:** [May 4, 2018, 10:16pm UTC](https://discourse.julialang.org/t/developing-pass-through-types/10717/2 "2018-05-04T22:16:36Z")

</div>

Could you simply make `struct Foo{T} <: AbstractVector{T}`?

---

<div class="post-metadata">

**Author:** ![ccoffrin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ccoffrin/32/400_2.png) [@ccoffrin](https://discourse.julialang.org/u/ccoffrin)\
**Post date:** [May 4, 2018, 10:21pm UTC](https://discourse.julialang.org/t/developing-pass-through-types/10717/3 "2018-05-04T22:21:21Z")

</div>

In this case wouldn’t Foo need to re-implement all of the implementation details that occur between an AbstractVector and a concrete Vector? Or have some generic pass-through method to delegate most function calls to its internal Vector?

---

<div class="post-metadata">

**Author:** ![Stephen\_Vavasis](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stephen_vavasis/32/3389_2.png) [@Stephen\_Vavasis](https://discourse.julialang.org/u/Stephen_Vavasis)\
**Post date:** [May 4, 2018, 10:55pm UTC](https://discourse.julialang.org/t/developing-pass-through-types/10717/4 "2018-05-04T22:55:18Z")

</div>

The section of the manual on “Code Generation” within the chapter on “Metaprogramming” gives an example of how to quickly generate operators for a new type in terms of known operators. I haven’t ever used this technique, so I can’t vouch for it.

---

<div class="post-metadata">

**Author:** ![mohamed82008](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mohamed82008/32/18171_2.png) [@mohamed82008](https://discourse.julialang.org/u/mohamed82008)\
**Post date:** [May 5, 2018, 12:43am UTC](https://discourse.julialang.org/t/developing-pass-through-types/10717/5 "2018-05-05T00:43:49Z")

</div>

It would be nice to have an ArrayWrappers package that has a macro which passes on the functions calls. Probably any of the array packages would be a good starting point to get the list of functions that need to be overloaded.

---

<div class="post-metadata">

**Author:** ![rdeits](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rdeits/32/286_2.png) [@rdeits](https://discourse.julialang.org/u/rdeits)\
**Post date:** [May 5, 2018, 1:18am UTC](https://discourse.julialang.org/t/developing-pass-through-types/10717/6 "2018-05-05T01:18:20Z")

</div>

I’ve found the `@forward` macro from Lazy.jl useful in cases like this: [https://github.com/MikeInnes/Lazy.jl/blob/271bdc04a02aa0d99d87c17ffed41c3a07d4fd57/src/macros.jl#L248-L271](https://github.com/MikeInnes/Lazy.jl/blob/271bdc04a02aa0d99d87c17ffed41c3a07d4fd57/src/macros.jl#L248-L271)

With `@forward` you still have to specify every function that you want to forward to the wrapped variable (so it’s not exactly what you’re looking for), but it still saves a fair amount of work.

---

<div class="post-metadata">

**Author:** ![JeffreySarnoff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jeffreysarnoff/32/1980_2.png) [@JeffreySarnoff](https://discourse.julialang.org/u/JeffreySarnoff)\
**Post date:** [May 5, 2018, 5:14am UTC](https://discourse.julialang.org/t/developing-pass-through-types/10717/7 "2018-05-05T05:14:50Z")

</div>

Yes, that’s a good one.

And there exists [TypedDelegation.jl]

```julia

import Base: show, string, signbit, sign, abs, div
 
 
struct DelegatedInt
    value::Int
end

# [show, string, signbit] evaluate as other types (not DelegatedInt)

@delegate_onefield( DelegatedInt, value, [show, string, signbit] )

# [sign, abs] evaluate as the given type (DelgatedInt)
                   
@delegate_onefield_astype( DelegatedInt, value, [sign, abs] )

# [div] takes two args and evaluates as the given type

@delegate_onefield_twovars_astype( DelegatedInt, value, [div] )

```

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [May 7, 2018, 8:06am UTC](https://discourse.julialang.org/t/developing-pass-through-types/10717/8 "2018-05-07T08:06:31Z")

</div>

> [@rdeits](#):
>
> you still have to specify every function that you want to forward to the wrapped variable

IMO that is the right trade-off for most cases, in terms of code readability/documentation. I can imagine a specialized solution for some interfaces (eg `<: Number`) which forwards a predefined set of functions, but for one-off solutions just enumerating is worth it when looking at that code 3 months later.

---

<div class="post-metadata">

**Author:** ![ScottPJones](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/scottpjones/32/146_2.png) [@ScottPJones](https://discourse.julialang.org/u/ScottPJones)\
**Post date:** [May 7, 2018, 8:42am UTC](https://discourse.julialang.org/t/developing-pass-through-types/10717/9 "2018-05-07T08:42:51Z")

</div>

I do hope that one of the goals post v1.0 will be to come up with some standard interface / protocol design for Julia (I’ve seen it discussed many times, but I’m not sure what the latest thinking is).  
I found it to be the biggest pain in writing a large (set of) packages for strings, that and trying to avoid ambiguity issues and type piracy.  
Sometimes you need to extend an exported method, but other times you need to extend an unexported `_*` method (like `_replace`, `_split`, `_rsplit`) to get things to work, and it’s not really documented.  
Finally, sometimes the only way to make things work so that things work as you’d expect is via completely replacing the Base structs and/or methods (that’s the case for regex support, because the macros such as `@r_str` create a `Regex` struct that is not generic, it only works for `String`)

---

<div class="post-metadata">

**Author:** ![oxinabox](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oxinabox/32/206603_2.png) [@oxinabox](https://discourse.julialang.org/u/oxinabox)\
**Post date:** [May 7, 2018, 9:14am UTC](https://discourse.julialang.org/t/developing-pass-through-types/10717/10 "2018-05-07T09:14:01Z")

</div>

These are the fields you need to overload,  
and this is how you overload them

[https://github.com/JuliaText/CorpusLoaders.jl/blob/1a011a08b6072242604b6fd2ebcdf5ed258b6f44/src/types.jl#L5-L23](https://github.com/JuliaText/CorpusLoaders.jl/blob/1a011a08b6072242604b6fd2ebcdf5ed258b6f44/src/types.jl#L5-L23)

That one is done by declaring an abstract type.  
But of-course you can do it directly,  
or via metaprogramming
