# Pre-SIP : Reintroduce view bounds for non-implicit conversions

**URL:** <https://contributors.scala-lang.org/t/pre-sip-reintroduce-view-bounds-for-non-implicit-conversions/4909>\
**Category:** Scala Improvement Process\
**Created:** [February 26, 2021, 9:14pm UTC](https://contributors.scala-lang.org/t/pre-sip-reintroduce-view-bounds-for-non-implicit-conversions/4909 "2021-02-26T21:14:08Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![rjolly](https://contributors.scala-lang.org/user_avatar/contributors.scala-lang.org/rjolly/32/1737_2.png) [@rjolly](https://contributors.scala-lang.org/u/rjolly)\
**Post date:** [February 26, 2021, 9:14pm UTC](https://contributors.scala-lang.org/t/pre-sip-reintroduce-view-bounds-for-non-implicit-conversions/4909/1 "2021-02-26T21:14:08Z")

</div>

Currently, view bounds are causing a deprecation error:

```scala
class A

def f[T <% A](x: T) = { val a: A = x; ??? }
        ^
        view bounds `<%' are deprecated, use a context bound `:' instead

```

However, this is what we get and it’s misleading:

```scala
def f[T : A](x: T) = ??? // that is:
def f[T](x: T)(using A[T]) = ???

```

What we want instead is:

```scala
import scala.language.implicitConversions
def f[T](x: T)(using Conversion[T, A]) = { val a: A = x; ??? }

```

Or, alternatively:

```scala
def f[T](x: T)(using T => A) = { val a = summon[T => A](x); ??? }

```

, which brings back the boilerplate the view bound was supposed to remove (maybe the reason it was deprecated?) On the other hand, this syntax is going to be more and more used as [we want to avoid implicit conversion](https://contributors.scala-lang.org/t/can-we-wean-scala-off-implicit-conversions/4388). Indeed, the recommended way to express this:

```scala
def f(a: A) = ???
given Conversion[String, A] = ???
import scala.language.implicitConversions
f("hello")

```

, is to apply the conversion explicitly and push it in the library code with a using clause:

```scala
def f[T](x: T)(using c: T => A) = { val a = c(x); ??? }

```

We may call this pattern _anticipated implicit conversion_ or _anticipated conversion_ for short. My proposal is to introduce an extension method `as` to simplify the conversion application:

```scala
extension [T](x: T)
  def as[A](using c: T => A) = c(x)

def f[T](x: T)(using T => A) = { val a = x.as[A]; ??? }

```

Or, in view bound syntax, which gets back all its relevance:

```scala
def f[T <% A](x: T) = { val a = x.as[A]; ??? }

```

---

<div class="post-metadata">

**Author:** ![sjrd](https://contributors.scala-lang.org/user_avatar/contributors.scala-lang.org/sjrd/32/24_2.png) [@sjrd](https://contributors.scala-lang.org/u/sjrd)\
**Post date:** [February 27, 2021, 4:07am UTC](https://contributors.scala-lang.org/t/pre-sip-reintroduce-view-bounds-for-non-implicit-conversions/4909/2 "2021-02-27T04:07:58Z")

</div>

I don’t understand how your final result

```scala
def f[T <% A](x: T) = { val a = x.as[A]; ??? }

```

is supposed to be better than the mentioned status quo

```scala
def f[T](x: T)(using c: T => A) = { val a = c(x); ??? }

```

---

<div class="post-metadata">

**Author:** ![rjolly](https://contributors.scala-lang.org/user_avatar/contributors.scala-lang.org/rjolly/32/1737_2.png) [@rjolly](https://contributors.scala-lang.org/u/rjolly)\
**Post date:** [February 27, 2021, 11:29am UTC](https://contributors.scala-lang.org/t/pre-sip-reintroduce-view-bounds-for-non-implicit-conversions/4909/3 "2021-02-27T11:29:54Z")

</div>

Well, it’s one parameter section less, and it doesn’t introduce a fresh variable. And it looks like a normal bound `T <: A`, which makes its meaning clearer - that is, we can expect to somehow being able to “upcast” `x` to `A`.

By that argument, context bounds too are useless.

Some historical context :

[https://groups.google.com/d/topic/scala-internals/hNow9MvAi6Q](https://groups.google.com/d/topic/scala-internals/hNow9MvAi6Q)  
[https://groups.google.com/d/topic/scala-sips/wP6dL8nIAQs](https://groups.google.com/d/topic/scala-sips/wP6dL8nIAQs)

Quoting Martin’s original argument:

> It seems perverse that we would have syntactic sugar for implicit conversions that we propose to put under a feature flag because they are so easily misused. So I propose to deprecate view bounds.

And indeed they were misused, mostly to model what is now covered by extension methods:

> A method with a view bound:
> 
> ```scala
> def foo[T <% Ordered[T]](x: T, y: T) = x < y
> 
> ```
> 
> To get rid of the view bound, define a type alias
> 
> ```scala
> type OrderedView[T] = T => Ordered[T]
> 
> ```
> 
> and rewrite foo to:
> 
> ```scala
> def foo[T: OrderedView](x: T, y: T) = x < y
> 
> ```

Nowadays we have the `Ordering` typeclass with its extension methods. However, there remains @lihaoyi’s “implicit constructor” use case which is _not_ subsumed by type classes (or context bounds). Together with Martin’s recommended way of using conversions (that is, not implicit), I think we still have a relevant use for the syntax. We just have to make sure conversion is not applied implicitly without the feature flag (or at all). I will need assistance for this, here is my pull request [Reintroduce view bounds for non-implicit conversions by rjolly · Pull Request #11559 · lampepfl/dotty · GitHub](https://github.com/lampepfl/dotty/pull/11559). Any help appreciated.

---

<div class="post-metadata">

**Author:** ![megri](https://contributors.scala-lang.org/user_avatar/contributors.scala-lang.org/megri/32/680_2.png) [@megri](https://contributors.scala-lang.org/u/megri)\
**Post date:** [March 1, 2021, 12:28pm UTC](https://contributors.scala-lang.org/t/pre-sip-reintroduce-view-bounds-for-non-implicit-conversions/4909/4 "2021-03-01T12:28:26Z")

</div>

I’m not sure I understand the use-case properly, but to be frank I’d rather see context bounds disappear as well. The convenience of them is diminished by the clarity and versatility of `using`.

---

<div class="post-metadata">

**Author:** ![rjolly](https://contributors.scala-lang.org/user_avatar/contributors.scala-lang.org/rjolly/32/1737_2.png) [@rjolly](https://contributors.scala-lang.org/u/rjolly)\
**Post date:** [March 1, 2021, 1:46pm UTC](https://contributors.scala-lang.org/t/pre-sip-reintroduce-view-bounds-for-non-implicit-conversions/4909/5 "2021-03-01T13:46:44Z")

</div>

It turns out conversions are not applied implicitly - probably due to the syntax being desugared not to `using Conversion[T, A]` but `T => A` which is perfectly fine. I’ve added a [neg test](https://github.com/lampepfl/dotty/blob/a5ae94b587c3f24b70797b9a3ced35f214457c77/tests/neg/t3688.scala) to be sure. The PR is ready for review.

Edit : maybe we should resume formal SIP for such change. Is [this page](https://docs.scala-lang.org/sips/sip-submission.html) still relevant?

Edit2 : I had to use [`convertTo`](https://github.com/lampepfl/dotty/pull/11559/commits/89ed54ca728d499238a0db6ab743c8cb916f5e1e) instead of `as`, which collides with several constructs (in compiler tests, cats stm, scalaz).

---

<div class="post-metadata">

**Author:** ![jducoeur](https://contributors.scala-lang.org/user_avatar/contributors.scala-lang.org/jducoeur/32/32_2.png) [@jducoeur](https://contributors.scala-lang.org/u/jducoeur)\
**Post date:** [March 1, 2021, 10:51pm UTC](https://contributors.scala-lang.org/t/pre-sip-reintroduce-view-bounds-for-non-implicit-conversions/4909/6 "2021-03-01T22:51:59Z")

</div>

I have to observe that this isn’t anything new. I mean, the community has been saying “don’t use view bounds, because they are going away” since (Googles) [at least 2013](https://github.com/scala/bug/issues/7629)…

---

<div class="post-metadata">

**Author:** ![AugustNagro](https://contributors.scala-lang.org/user_avatar/contributors.scala-lang.org/augustnagro/32/2660_2.png) [@AugustNagro](https://contributors.scala-lang.org/u/AugustNagro)\
**Post date:** [March 2, 2021, 2:49am UTC](https://contributors.scala-lang.org/t/pre-sip-reintroduce-view-bounds-for-non-implicit-conversions/4909/7 "2021-03-02T02:49:33Z")

</div>

> [@megri](#):
>
> I’m not sure I understand the use-case properly, but to be frank I’d rather see context bounds disappear as well. The convenience of them is diminished by the clarity and versatility of `using`.

I’d love to see context bounds removed too. The beginner confusion is not worth being slightly less verbose. However I also wish Scala had the ‘break’ keyword, so maybe don’t listen to me.

---

<div class="post-metadata">

**Author:** ![rjolly](https://contributors.scala-lang.org/user_avatar/contributors.scala-lang.org/rjolly/32/1737_2.png) [@rjolly](https://contributors.scala-lang.org/u/rjolly)\
**Post date:** [March 2, 2021, 9:08am UTC](https://contributors.scala-lang.org/t/pre-sip-reintroduce-view-bounds-for-non-implicit-conversions/4909/8 "2021-03-02T09:08:51Z")

</div>

I admit the improvement is not huge, but think that it might be all we are left with once implicit conversions are phased out.

---

<div class="post-metadata">

**Author:** ![sjrd](https://contributors.scala-lang.org/user_avatar/contributors.scala-lang.org/sjrd/32/24_2.png) [@sjrd](https://contributors.scala-lang.org/u/sjrd)\
**Post date:** [March 2, 2021, 9:26am UTC](https://contributors.scala-lang.org/t/pre-sip-reintroduce-view-bounds-for-non-implicit-conversions/4909/9 "2021-03-02T09:26:00Z")

</div>

Definitely not the only thing left, since, as mentioned above, we can do

```scala
def f[T](x: T)(using c: T => A) = { val a = c(x); ??? }

```

and we can also do

```scala
def f[T](x: T)(using c: Conversion[T, A]) = { val a = c(x); ??? }

```

Both would work even if implicit conversions were completely phased out and `Conversion` was not special anymore.

Again, reintroducing `<%` only brings more incentive to use more implicit conversions, without adding any expressiveness. IMO this is not going to fly.

---

<div class="post-metadata">

**Author:** ![rjolly](https://contributors.scala-lang.org/user_avatar/contributors.scala-lang.org/rjolly/32/1737_2.png) [@rjolly](https://contributors.scala-lang.org/u/rjolly)\
**Post date:** [March 2, 2021, 9:31am UTC](https://contributors.scala-lang.org/t/pre-sip-reintroduce-view-bounds-for-non-implicit-conversions/4909/10 "2021-03-02T09:31:38Z")

</div>

Using more implicit conversions would mean stay with this scheme:

```scala
def f(a: A) = ???
f("hello")

```

On the contrary, my proposal is to nuge people towards the recommended construct.

My problem with your syntax is mainly the fresh variable `c`. Either we use it systematically by convention, and `c` is forever unavailable for other uses in the method body. Or we are left with bothering with an original name at all times.

---

<div class="post-metadata">

**Author:** ![sjrd](https://contributors.scala-lang.org/user_avatar/contributors.scala-lang.org/sjrd/32/24_2.png) [@sjrd](https://contributors.scala-lang.org/u/sjrd)\
**Post date:** [March 2, 2021, 9:49am UTC](https://contributors.scala-lang.org/t/pre-sip-reintroduce-view-bounds-for-non-implicit-conversions/4909/11 "2021-03-02T09:49:45Z")

</div>

> [@rjolly](#):
>
> My problem with your syntax is mainly the fresh variable `c`.

So you’re trading the need for a fresh local variable name for an additional language construct? That’s way below the bar for having a construct in the language, in my book.

---

<div class="post-metadata">

**Author:** ![rjolly](https://contributors.scala-lang.org/user_avatar/contributors.scala-lang.org/rjolly/32/1737_2.png) [@rjolly](https://contributors.scala-lang.org/u/rjolly)\
**Post date:** [March 2, 2021, 10:14am UTC](https://contributors.scala-lang.org/t/pre-sip-reintroduce-view-bounds-for-non-implicit-conversions/4909/12 "2021-03-02T10:14:54Z")

</div>

Why, it would enforce a conventional name, like `convertTo` (unfortunately, `as` seems already in use) and alleviate the burden of coming up with one each time. We already do this with `summon`, to allow anonymous givens. In a sense, I just propose to add a shorthand for `summon[T => A]`.

I think one problem is that this syntactic question is intertwined with the relevance of implicit conversions as such (in the “implicit contructor” sense), so let me elaborate on why I think these are still important. As you may have noticed, I am trying to use Scala to do math. In math, objects tend to be in a subset relationship, like Integers is a subset of Rationals and so on. What we are looking for is a substitution principle, whereby each time a rational is needed (which you can specify with a bound, `T <: Rational`), you can substitute an integer. OOP is calamitous in that respect, as it works the other way around : what you typically have is that `Rational extends Integer`, which means you can substitute a rational for an integer, which does not make mathematical sense.

And you can’t use `Integer extends Rational`, because that would mean to know every possible superset in advance, which is clearly impossible.

Enter implicit conversions (or views):

```scala
given int2rational: (Integer => Rational) = ???

```

, and you have your substitution principle. This time your requirement is expressed with a view bound `T <% Rational` rather than a normal, subtype bound. But otherwise it works about the same.

So, that’s why I think the syntax is important, irrespective to conversions being applied implicitly or not (and the prospect is going to be “not” as far as I can tell). It would make the pattern a first-class citizen, on a par with type classes and their special context bound syntax (`:`) which everybody agrees is important and an improvement w.r.t. the `using` syntax in terms of level of abstraction.

---

<div class="post-metadata">

**Author:** ![morgen-peschke](https://contributors.scala-lang.org/user_avatar/contributors.scala-lang.org/morgen-peschke/32/2528_2.png) [@morgen-peschke](https://contributors.scala-lang.org/u/morgen-peschke)\
**Post date:** [March 2, 2021, 6:41pm UTC](https://contributors.scala-lang.org/t/pre-sip-reintroduce-view-bounds-for-non-implicit-conversions/4909/13 "2021-03-02T18:41:08Z")

</div>

At the risk of adding an extra abstraction, you can make this relationship explicit (and still fairly low boilerplate)

```auto
object definitions {  
  trait Subset[A, B] {
    def witness(a: A): B
  }

  object Subset {
    // This is supposed to work (see `sumBy` example here: https://dotty.epfl.ch/docs/reference/contextual/extension-methods.html#generic-extensions)
    //extension [A](a: A) {
    // def is[B](using S: Subset[A, B]): B = S.witness(a)
    //}
    implicit class Ops[A](a: A) extends AnyVal {
      def is[B](using S: Subset[A, B]): B = S.witness(a)
    }
  }
}

object example {
  import definitions.Subset, Subset.Ops
  
  case class Rational(numerator: Int, denominator: Int)

  given Subset[Int, Rational] with {
    def witness(i: Int): Rational = Rational(i, 1)
  }
  
  def foo[A](a: A)(using A Subset Rational): Rational = 
    a.is[Rational]
  
  // For the ones you use a lot
  type IsRational[A] = Subset[A, Rational]
  def bar[A: IsRational](a: A): Rational = 
    a.is[Rational]
  
  @main def test() = {
    println(foo(1))
    println(bar(2))
  }
}
```

[Scastie](https://scastie.scala-lang.org/KBe3u5PAQzquBBUfMq9CYA)

What I think this alternative hints at is that this is a more general case of running into the limitation that context bounds can only have a single type parameter. If context bounds could be written using an anonymous type parameter, it’d be much more usable:

```auto
def foo[A: Subset[_, Rational]](a: A): Rational = a.is[Rational]
```

Leveraging infix types as in the example above, it’d be even nicer:

```auto
def foo[A: _ Subset Rational](a: A): Rational = a.is[Rational]
```

---

<div class="post-metadata">

**Author:** ![rjolly](https://contributors.scala-lang.org/user_avatar/contributors.scala-lang.org/rjolly/32/1737_2.png) [@rjolly](https://contributors.scala-lang.org/u/rjolly)\
**Post date:** [March 2, 2021, 7:32pm UTC](https://contributors.scala-lang.org/t/pre-sip-reintroduce-view-bounds-for-non-implicit-conversions/4909/14 "2021-03-02T19:32:27Z")

</div>

I thinks it’s more or less isomorphic to what I propose, minus the `<%` notation:

```scala
extension [T](x: T)
  def convertTo[A](using c: T => A) = c(x)

def foo[A](a: A)(using A => Rational): Rational = 
    a.convertTo[Rational]

type IsRational[A] = A => Rational
def bar[A: IsRational](a: A): Rational = 
  a.convertTo[Rational]

```

---

<div class="post-metadata">

**Author:** ![odersky](https://contributors.scala-lang.org/user_avatar/contributors.scala-lang.org/odersky/32/42_2.png) [@odersky](https://contributors.scala-lang.org/u/odersky)\
**Post date:** [March 2, 2021, 8:58pm UTC](https://contributors.scala-lang.org/t/pre-sip-reintroduce-view-bounds-for-non-implicit-conversions/4909/15 "2021-03-02T20:58:26Z")

</div>

Scala has been accused of having an excessive number of weird symbolic operators. I don’t think that’s true (at least not for the language proper), but nevertheless we should strive to remove them where we can. So `<%` is gone, and that’s a good thing!

---

<div class="post-metadata">

**Author:** ![morgen-peschke](https://contributors.scala-lang.org/user_avatar/contributors.scala-lang.org/morgen-peschke/32/2528_2.png) [@morgen-peschke](https://contributors.scala-lang.org/u/morgen-peschke)\
**Post date:** [March 2, 2021, 9:45pm UTC](https://contributors.scala-lang.org/t/pre-sip-reintroduce-view-bounds-for-non-implicit-conversions/4909/16 "2021-03-02T21:45:02Z")

</div>

@rjolly : Yep, the encoding I used wasn’t anything particularly unique, my only insight was that if context bounds could handle an anonymous placeholder, the difference between `<%` and the alternatives shrinks considerably.

@odersky : is an anonymous placeholder in context bounds a feasible thing to explore at some later date?

---

<div class="post-metadata">

**Author:** ![rjolly](https://contributors.scala-lang.org/user_avatar/contributors.scala-lang.org/rjolly/32/1737_2.png) [@rjolly](https://contributors.scala-lang.org/u/rjolly)\
**Post date:** [March 2, 2021, 9:54pm UTC](https://contributors.scala-lang.org/t/pre-sip-reintroduce-view-bounds-for-non-implicit-conversions/4909/17 "2021-03-02T21:54:47Z")

</div>

Ok, I get it. Indeed these two are equivalent:

```scala
def foo[T : _ => A](x: T) = { val a = x.as[T] ; ??? }
def foo[T <% A](x: T) = { val a = x.as[T] ; ??? }

```

Edit : problem solved:

```scala
type <%%[X] = [T] =>> T => X
def foo[T: <%%[A]](x: T) = { val a = x.as[A] ; ??? }

```

---

<div class="post-metadata">

**Author:** ![Jasper-M](https://contributors.scala-lang.org/user_avatar/contributors.scala-lang.org/jasper-m/32/1192_2.png) [@Jasper-M](https://contributors.scala-lang.org/u/Jasper-M)\
**Post date:** [March 3, 2021, 10:30am UTC](https://contributors.scala-lang.org/t/pre-sip-reintroduce-view-bounds-for-non-implicit-conversions/4909/18 "2021-03-03T10:30:25Z")

</div>

> [@morgen-peschke](#):
>
> is an anonymous placeholder in context bounds a feasible thing to explore at some later date?

Using a type lambda is already possible:

```auto
def foo[A: [x] =>> Conversion[x, Int]](a: A): Int = a

```

And I thought the plan was for `Conversion[_, A]` to mean `[x] =>> Conversion[x, A]` somewhere in the future. That’s why wildcard types are now written with `?` instead of `_`.

---

<div class="post-metadata">

**Author:** ![morgen-peschke](https://contributors.scala-lang.org/user_avatar/contributors.scala-lang.org/morgen-peschke/32/2528_2.png) [@morgen-peschke](https://contributors.scala-lang.org/u/morgen-peschke)\
**Post date:** [March 3, 2021, 5:20pm UTC](https://contributors.scala-lang.org/t/pre-sip-reintroduce-view-bounds-for-non-implicit-conversions/4909/19 "2021-03-03T17:20:46Z")

</div>

> [@rjolly](#):
>
> Edit : problem solved:
> 
> ```auto
> type <%%[X] = [T] =>> T => X
> def foo[T: <%%[A]](x: T) = { val a = x.as[A] ; ??? }
> 
> ```

Clever! That’ll definitely work for now.

I might suggest this alternative:

```auto
type %%>[O] = [I] =>> I => O

```

That way the arrows follow the transformation (`T` points to `A`, suggesting that `T` becomes `A`):

```auto
def foo[T: %%>[A]](x: T) = { val a = x.as[A] ; ??? }

```

---

<div class="post-metadata">

**Author:** ![rjolly](https://contributors.scala-lang.org/user_avatar/contributors.scala-lang.org/rjolly/32/1737_2.png) [@rjolly](https://contributors.scala-lang.org/u/rjolly)\
**Post date:** [March 3, 2021, 8:58pm UTC](https://contributors.scala-lang.org/t/pre-sip-reintroduce-view-bounds-for-non-implicit-conversions/4909/20 "2021-03-03T20:58:06Z")

</div>

Yes, I’m leaning towards something more explicit, like:

```scala
type Conversion[A] = [X] =>> X => A
def foo[T: Conversion[A]](x: T) = { val a = x.as[A] ; ??? }

```

Anyway, there’s no need to embed this in the language anymore. Everyone can choose what they see fit.
