1

I recently upgraded to Scala 2.13, and now am being warned about a deprecation. My function looks like so:

implicit def convertGeneralResult[A <% ToResponseMarshallable, B <% ToResponseMarshallable](r: Either[A, B]) =
  r.fold[ToResponseMarshallable](identity, identity)

The deprecation warning reads (I actually have two of them, one for each type parameter A/B):

view bounds are deprecated; use an implicit parameter instead.
example: instead of def f[A <% Int](a: A) use def f[A](a: A)(implicit ev: A => Int)

I'm not entirely sure how to apply the suggested fix in my case though.

Dmytro Mitin
  • 48,194
  • 3
  • 28
  • 66
ThaDon
  • 7,826
  • 9
  • 52
  • 84
  • Note that **implicit conversions** _(which is what **view bounds** are)_ are considered a bad practice, it would be good to try to redactor that code to do not depend on that. – Luis Miguel Mejía Suárez Jan 05 '21 at 13:12

1 Answers1

4

There are many depths into this question, but I'll give only some references in case you want to deep dive into it, and how to fix it as well.

Already in 2013, in Scala 2.11 you can find a bug in Scala called deprecation warning for view bounds under -Xfuture. At the time this was implemented, but in case you didn't use the -Xfuture option in SCALA COMPILER OPTIONS you weren't aware of that.

In 2018 Deprecate view bounds without -Xfuture was opened, which was merged into Scala 2.13 as you noticed.

As another reference, there is a similar question Scala 2.13.0 deprecates <%, but how do I get rid of this in a class definition.

Now to your issue. All you need to do is remove the <% usage, and replace those with implicits:

implicit def convertGeneralResult[A, B](r: Either[A, B])(implicit aToMarshallable: A => ToResponseMarshallable, bToMarshallable: B => ToResponseMarshallable) =
  r.fold[ToResponseMarshallable](identity, identity)
Tomer Shetah
  • 8,413
  • 7
  • 27
  • 35
  • Oh I see! I was confused in that I thought I had to actually *explicitly* invoke the implicit parameter within my function to convert the parameters to fold. But I don’t have to, it’s enough to to simply declare the implicits, which implies there must exist an implicit that takes the types from A/B to `ResponseMarshallable[A/B]`. Is that a correct interpretation? – ThaDon Jan 05 '21 at 10:34
  • You can refer to [this](https://stackoverflow.com/q/4465948/2359227) or [this](https://stackoverflow.com/a/11516961/2359227) post for more information. Basically it means that `A`/`B` can convert to `ToResponseMarshallable`. – Tomer Shetah Jan 05 '21 at 10:44
  • 1
    Oh yes, sorry. `ToResponseMarsallable` as you said (and without the type parameter) :) – ThaDon Jan 05 '21 at 10:50