# Design patterns for composability with traits (i.e. typeclasses)?

**URL:** <https://users.rust-lang.org/t/design-patterns-for-composability-with-traits-i-e-typeclasses/5569>\
**Category:** help\
**Created:** [April 26, 2016, 8:21am UTC](https://users.rust-lang.org/t/design-patterns-for-composability-with-traits-i-e-typeclasses/5569 "2016-04-26T08:21:17Z")\
**Posts on this page:** 1\
**Showing post:** 5

<div class="post-metadata">

**Author:** ![shelby3](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/shelby3/32/2381_2.png) [@shelby3](https://users.rust-lang.org/u/shelby3)\
**Post date:** [April 27, 2016, 3:32pm UTC](https://users.rust-lang.org/t/design-patterns-for-composability-with-traits-i-e-typeclasses/5569/5 "2016-04-27T15:32:01Z")

</div>

Afaics, multiple passes of `Enumerable:fold` can also bubble sort and I suppose other sort algorithms. But that is not particularly germane to issue of this thread.

> [@keean](#):
>
> and that is don't put constraints on types, as it results in requiring that trait for every function you pass that type to, and does not provide that trait automatically.

I can't visualize unambiguously what you mean. Perhaps a code example or explaining what in the current example you are referring to specifically?

> [@keean](#):
>
> The general rule is to require the traits on the function that needs them

Oh so you mean for example the type of `R`? But I propose a `Comparable` trait instead below.

> [@keean](#):
>
> The thing to note here is that the projection function 'proj' is applied to each member before summing, so this is generic over any container which can contain a **heterogeneous** collection of any object types

So if the `ValueType` is a tagged union `enum`, does Rust know how to dynamic dispatch to the trait implementation for each possible data type in union? Edit: I see the [answer is yes](https://users.rust-lang.org/t/high-order-function-with-type-parameter/3112/184).

I assume yes, but afaics the problem remains that the `enum` is not first-class (so not extensible), so I can't change the `ValueType` [when adding](https://users.rust-lang.org/t/does-rust-really-need-higher-kinded-types/5531/32) new values to the collection (unless I rebuild a copy of the collection on every add which is intolerable).

> [@keean](#):
>
> where ... R : FnMut(&I::ValueType, &I::ValueType) -\> bool

`ValueType` is a type parameter on the parameterized type `I` so afaics it is a HKT?

I'd prefer to see a `Comparable` trait here instead, but I don't know to specify that the associated type `ValueType` is bounded on the trait `Comparable` in Rust.

> [@keean](#):
>
> If you wanted to combine multiple traits, then you would do something like:
> 
> `trait Shape : HasArea + OtherTraits`

Afaics, the problem again is this is nominal and not first-class composability, thus afaics [requires additional boilerplate](https://users.rust-lang.org/t/high-order-function-with-type-parameter/3112/183).

---

_[View the full topic](https://users.rust-lang.org/t/design-patterns-for-composability-with-traits-i-e-typeclasses/5569)._
