# High Order Function with Type Parameter

**URL:** <https://users.rust-lang.org/t/high-order-function-with-type-parameter/3112>\
**Category:** help\
**Created:** [October 3, 2015, 3:59am UTC](https://users.rust-lang.org/t/high-order-function-with-type-parameter/3112 "2015-10-03T03:59:59Z")\
**Posts on this page:** 1\
**Showing post:** 8

<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 20, 2016, 10:40am UTC](https://users.rust-lang.org/t/high-order-function-with-type-parameter/3112/8 "2016-04-20T10:40:07Z")

</div>

> [@DroidLogician](#):
>
> We do have higher-ranked trait bounds but they currently only work for lifetimes. If they worked for type parameters you could do something like this:
> 
> It's probably on the table of features to implement one day, likely as part of the broader Higher Kinded Types initiative.
> 
> Automatically implementing `Foo` for closures would require the same higher-kinded polymorphism, AFAIK, so that's not really possible either.

> [@Yuhta](#):
>
> And in Haskell this problem can be solved using existential type:
> 
> `{-# LANGUAGE Rank2Types #-}`
> 
> `f :: (forall a. (Num a) =&gt; a -&gt; t) -&gt; Bool -&gt; t`  
> `f g p = if p then g (1 :: Int) else g (1.1 :: Double)`
> 
> Is there similar thing in Rust?

I understand that in Rust, all (and only) lambda expressions [are closures](https://doc.rust-lang.org/reference.html#closure-types); and closures [are trait types](https://doc.rust-lang.org/book/closures.html#closure-implementation) such as `Fn`.

I understand we can emulate rank-2 (higher ranked) types in a language which doesn't explicitly support them via `forall`, by employing a trait with a method for generic parameter `A`, as explained in [an example for Scala](https://apocalisp.wordpress.com/2010/07/02/higher-rank-polymorphism-in-scala/).

Compared to that linked Scala example, the example in this thread is **not requiring a higher ranked type** , because the `Read` trait lower bound is constant throughout the function `f`. Also afaics, the example in this thread **does not require a higher-kinded type** on the trait `Fn` in order to parameterize the lower bound on the type parameter `A` of `Fn`, i.e. requires a `trait Fn<A>` to be of the type `trait Fn<Read>`. Higher-kinded types are of the form `trait HK<A, B<A>>`.

> [@daboross](#):
>
> In other words, since `A` is generic, the caller defines `A`. You _have_ to provide the right `A`, you can't just choose one thing which implements `Read`, because `G` may be a function which will _only_ accept a `Cursor`, or a `TcpStream`, etc.

Agreed. The reason the example of this thread won't compile is not due to a lack of higher-kinded nor higher-ranked types, but rather because afaik the way Rust implements ad hoc polymorphism is it creates a separate instance of each function for each caller that uses a different set of input types, i.e. Rust's implementation is not a dynamic dispatch of passing in a hidden input argument for the dictionary for the trait implementation of the caller and is instead hard-coding the dictionary in each instance of the function, e.g. is not subclassing nor dynamic inversion-of-control ad hoc polymorphism (where the caller inputs the dictionary). Thus the function `f` has no way to control the types of `Read` instance of `G` it wants unless it specifies them. If `f` knows all the specific instance of `Read` it will use, then it can specify them as the lower bound on `A` instead of `Read`, except there is the problem that Rust (nor Haskell, nor Scala, nor Kotlin, etc) does not support first-class disjunctions (aka unions) thus we can't write:

`fn f<A: FileHandle | Cursor, G: Fn(A) -> ()>(g: G, pred: bool) {`

> [@sfackler](#):
>
> It's saying "for every pair of types `A` which is `Read` and `G` which is `Fn(A)`, there exists a function f that does this...". I don't know if there's any way to do what you want directly with the `Fn` trait. You'd really need to "lower" the `A` type parameter from the trait itself onto the trait's method. Something like this will work, for example

So thus this is the only way to implement this example in Rust.

---

_[View the full topic](https://users.rust-lang.org/t/high-order-function-with-type-parameter/3112)._
