# Enable all functions to work on element of new type

**URL:** <https://discourse.julialang.org/t/enable-all-functions-to-work-on-element-of-new-type/103210>\
**Category:** General Usage\
**Tags:** data\_structures, dispatch, composition\
**Created:** [August 25, 2023, 10:19pm UTC](https://discourse.julialang.org/t/enable-all-functions-to-work-on-element-of-new-type/103210 "2023-08-25T22:19:10Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![s-baumann](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/s-baumann/32/5843_2.png) [@s-baumann](https://discourse.julialang.org/u/s-baumann)\
**Post date:** [August 25, 2023, 10:19pm UTC](https://discourse.julialang.org/t/enable-all-functions-to-work-on-element-of-new-type/103210/1 "2023-08-25T22:19:10Z")

</div>

If I make a new struct that contains a certain type (a DataFrame in below example) like:

```julia
struct StructContainingDataFrame
    df::DataFrame
    something_else::Any
end

```

Is it then possible to make this work with all functions that are defined for a DataFrame input. In other words is it possible to get something like this to work:

```julia
function fff(x::StructContainingDataFrame) where fff in [DataFrames.nrow, DataFrames.ncol and all other 1d dataframes functions]
    fff(x.df)
end

```

where in the above case I would hope that any function supporting dataframes would also support my new struct.

---

<div class="post-metadata">

**Author:** ![bertschi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bertschi/32/33462_2.png) [@bertschi](https://discourse.julialang.org/u/bertschi)\
**Post date:** [August 25, 2023, 10:37pm UTC](https://discourse.julialang.org/t/enable-all-functions-to-work-on-element-of-new-type/103210/2 "2023-08-25T22:37:23Z")

</div>

Guess its only possible, if `DataFrames` does not rely on duck-typing, i.e., defines an abstract type (or a trait) that you could subtype.

On the other hand, what should those functions do on your new type? If you just forward them as in your example, i.e., `fff(x.df)`, the result would lose your type and `something_else` immediately after a single call. Thus, you might want to wrap via `fff(x::StructContainingDataFrame) = StructContainingDataFrame(fff(x.df), something_else)` and would need to redefine every function anyways.

PS: Seems that `DataFrames` does define `AbstractDataFrame`, but I don’t know which minimal interface is required to make all other methods defined for `AbstractDataFrame` work on your type.

---

<div class="post-metadata">

**Author:** ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)\
**Post date:** [August 25, 2023, 11:09pm UTC](https://discourse.julialang.org/t/enable-all-functions-to-work-on-element-of-new-type/103210/3 "2023-08-25T23:09:54Z")

</div>

See here

> [@General method for new structure](https://discourse.julialang.org/t/general-method-for-new-structure/51666/2):
>
> 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)

And the other thread linked therein.

For the specific example maybe go the other way and store whatever else you need a metadata of the DataFrame?
