# Better support for wrapper types

**URL:** <https://discourse.julialang.org/t/better-support-for-wrapper-types/114395>\
**Category:** Internals & Design\
**Created:** [May 17, 2024, 1:11pm UTC](https://discourse.julialang.org/t/better-support-for-wrapper-types/114395 "2024-05-17T13:11:49Z")\
**Posts on this page:** 1\
**Showing post:** 2

<div class="post-metadata">

**Author:** ![mikmoore](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mikmoore/32/31109_2.png) [@mikmoore](https://discourse.julialang.org/u/mikmoore)\
**Post date:** [May 17, 2024, 2:17pm UTC](https://discourse.julialang.org/t/better-support-for-wrapper-types/114395/2 "2024-05-17T14:17:06Z")

</div>

See [this discussion](https://discourse.julialang.org/t/forward-all-function-calls-on-mytype-to-another-wrapped-field/105750) for further discussion and, in particular, its recommendation of ForwardMethods.jl.

While sometimes I think about forwarding literally all methods, I usually end up deciding that’s too broad a brush. It also tends to break down. For example, if you wrap a `<:Number`, you might want `sqrt` to unwrap the value, apply `sqrt` and re-wrap it. But you don’t want to do this for another function like `>=(0)`, since you want that to return a `Bool`.

While doing this only for all functions explicitly defined for the wrapped type sounds more sensible, recall that many methods are duck-typed, so that list is very incomplete (for example, it would miss any of the many methods that take arbitrary arguments and `promote` them).

If you aren’t using a package’s macro but instead defining forwards manually for an explicit list, metaprogramming can be useful:

```julia
# unary functions
for f in [:abs, :abs2, :sqrt, :exp, :log, :sin, :cos, :tan] # and many more
	@eval Base.$f(x::MyType) = MyType(Base.$f(x.value))
end

# binary functions
for f in [:+, :-, :*, :/, :^] # and many more
	@eval Base.$f(x::MyType, y::Number) = MyType(Base.$f(x.value, y))
	@eval Base.$f(x::Number, y::MyType) = MyType(Base.$f(x, y.value))
	@eval Base.$f(x::MyType, y::Number) = MyType(Base.$f(x.value, y.value))
end

```

---

_[View the full topic](https://discourse.julialang.org/t/better-support-for-wrapper-types/114395)._
