# Implicit class for Any and/or generic type

**URL:** <https://users.scala-lang.org/t/implicit-class-for-any-and-or-generic-type/501>\
**Category:** Question\
**Created:** [May 22, 2017, 9:15pm UTC](https://users.scala-lang.org/t/implicit-class-for-any-and-or-generic-type/501 "2017-05-22T21:15:20Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![anthony.cros](https://users.scala-lang.org/user_avatar/users.scala-lang.org/anthony.cros/32/2036_2.png) [@anthony.cros](https://users.scala-lang.org/u/anthony.cros)\
**Post date:** [May 22, 2017, 9:15pm UTC](https://users.scala-lang.org/t/implicit-class-for-any-and-or-generic-type/501/1 "2017-05-22T21:15:20Z")

</div>

What do people think of that:

```
object Foo {

  implicit class AnythingImplicits[A](a: A /* TODO: or best use Any? */) {
    def then[B](f: A =B) = f(a) // or maybe "chain"?
  }

  val studies: Seq[Study] =
    studesJsonArrayString
      .then(Json.parse)
      .then(Json.fromJson[Seq[Study]])
      .get

  // ...
}

```

I find it easier to use than nesting the calls, especially if I often need to comment out certain steps (I can then just comment out the relevant line in the chain). I do worry about a performance penalty though…

Thoughts anyone? Does it exist in a library somewhere?

Thanks!

EDIT: fixed code block style  
EDIT2: removed noise from example

---

<div class="post-metadata">

**Author:** ![tpolecat](https://users.scala-lang.org/user_avatar/users.scala-lang.org/tpolecat/32/49_2.png) [@tpolecat](https://users.scala-lang.org/u/tpolecat)\
**Post date:** [May 22, 2017, 10:35pm UTC](https://users.scala-lang.org/t/implicit-class-for-any-and-or-generic-type/501/2 "2017-05-22T22:35:54Z")

</div>

Yep, this is fine, and it’s a thing! It’s called the thrush combinator. It’s provided by scalaz and by mouse (for cats) as `|>`.

```auto
scala> def add1(n: Int) = n + 1
add1: (n: Int)Int

scala> def twice(n: Int) = n * 2
twice: (n: Int)Int

scala> 3 |> add1 |> twice |> add1
res1: Int = 9

```

You do want to use `[A]` in your implementation, otherwise `f` would need to accept `Any`.

---

<div class="post-metadata">

**Author:** ![anthony.cros](https://users.scala-lang.org/user_avatar/users.scala-lang.org/anthony.cros/32/2036_2.png) [@anthony.cros](https://users.scala-lang.org/u/anthony.cros)\
**Post date:** [May 23, 2017, 1:14am UTC](https://users.scala-lang.org/t/implicit-class-for-any-and-or-generic-type/501/3 "2017-05-23T01:14:06Z")

</div>

Interesting, thanks! also I didn’t know about the mouse/cats libraries, so it’s good to know. I do find symbols like “|\>” pretty off-putting though, I really wish they’d also offer a more explicit counterpart…

---

<div class="post-metadata">

**Author:** ![tpolecat](https://users.scala-lang.org/user_avatar/users.scala-lang.org/tpolecat/32/49_2.png) [@tpolecat](https://users.scala-lang.org/u/tpolecat)\
**Post date:** [May 23, 2017, 4:07am UTC](https://users.scala-lang.org/t/implicit-class-for-any-and-or-generic-type/501/4 "2017-05-23T04:07:33Z")

</div>

Mouse also calls it `thrush` and scalaz also calls it `▹` … neither is terribly enlightening.

---

<div class="post-metadata">

**Author:** ![dwijnand](https://users.scala-lang.org/user_avatar/users.scala-lang.org/dwijnand/32/623_2.png) [@dwijnand](https://users.scala-lang.org/u/dwijnand)\
**Post date:** [June 14, 2017, 8:27am UTC](https://users.scala-lang.org/t/implicit-class-for-any-and-or-generic-type/501/5 "2017-06-14T08:27:32Z")

</div>

Tracked as [https://github.com/scala/bug/issues/5324](https://github.com/scala/bug/issues/5324)

---

<div class="post-metadata">

**Author:** ![anthony.cros](https://users.scala-lang.org/user_avatar/users.scala-lang.org/anthony.cros/32/2036_2.png) [@anthony.cros](https://users.scala-lang.org/u/anthony.cros)\
**Post date:** [February 16, 2021, 3:10pm UTC](https://users.scala-lang.org/t/implicit-class-for-any-and-or-generic-type/501/6 "2021-02-16T15:10:26Z")

</div>

For anyone who ends up here in 2021, it looks like this is now called `pipe` in [scala 2.13](https://alvinalexander.com/scala/scala-2.13-pipe-tap-chaining-operations/). Very glad this was added!

It’s unfortunate it requires an explicit import however, as I have ~500 calls to my `then` method (now `thn`) in my [codebase](https://github.com/galliaproject/gallia-core/blob/master/src/main/scala/gallia/atoms/AtomsUUTransforms.scala#L14). Maybe a scalac flag would be a good idea?

Another chain-like method I find really convenient is

`def thnIf(test: Boolean)(f: A => A): A = if (test) f(a) else a`

as used [here](https://github.com/galliaproject/gallia-core/blob/master/src/main/scala/gallia/io/IoType.scala#L78) for example.

---

<div class="post-metadata">

**Author:** ![Jasper-M](https://users.scala-lang.org/user_avatar/users.scala-lang.org/jasper-m/32/872_2.png) [@Jasper-M](https://users.scala-lang.org/u/Jasper-M)\
**Post date:** [February 17, 2021, 9:28am UTC](https://users.scala-lang.org/t/implicit-class-for-any-and-or-generic-type/501/7 "2021-02-17T09:28:54Z")

</div>

> [@anthony.cros](#):
>
> Maybe a scalac flag would be a good idea?

Not perfect but you can add to your build:

```auto
scalacOptions += "-Yimports:java.lang,scala,scala.Predef,scala.util.chaining"

```

---

<div class="post-metadata">

**Author:** ![anthony.cros](https://users.scala-lang.org/user_avatar/users.scala-lang.org/anthony.cros/32/2036_2.png) [@anthony.cros](https://users.scala-lang.org/u/anthony.cros)\
**Post date:** [February 17, 2021, 2:00pm UTC](https://users.scala-lang.org/t/implicit-class-for-any-and-or-generic-type/501/8 "2021-02-17T14:00:17Z")

</div>

That’s a neat trick actually, will do fine for the time being. Thanks!

---

<div class="post-metadata">

**Author:** ![anthony.cros](https://users.scala-lang.org/user_avatar/users.scala-lang.org/anthony.cros/32/2036_2.png) [@anthony.cros](https://users.scala-lang.org/u/anthony.cros)\
**Post date:** [May 31, 2021, 2:02pm UTC](https://users.scala-lang.org/t/implicit-class-for-any-and-or-generic-type/501/9 "2021-05-31T14:02:53Z")

</div>

> [@anthony.cros](#):
>
> `def thnIf(test: Boolean)(f: A => A): A = if (test) f(a) else a`

This is now part of my new open-source utilities library: [GitHub - aptusproject/aptus-core: A utility library aiming to simplify the Scala coding experience.](https://github.com/aptusproject/aptus-core#conditional-piping-aka-thrush), which I still need to formally announce (after docs are fleshed out)
