scala / scala3

The Scala 3 compiler, also known as Dotty.
https://dotty.epfl.ch
Apache License 2.0
5.84k stars 1.05k forks source link

can't summon useful Conversion instances #5967

Open drdozer opened 5 years ago

drdozer commented 5 years ago

I started building some code around Conversion but rapidly hit issues because:

infer[Conversion[String, String]]

no implicit argument of type Conversion[String, String] was found for parameter x of method infer in object DottyPredef.

It also doesn't seem to understand inheritance. This makes it unusable in practice. Have I done something daft?

odersky commented 5 years ago

The problem is that the standard Predef instances that handle subtyping conversions return <:< instances. <:< extends Function1 but not Conversion. We'll probably have to wait for full bootstrap before we can fix this, since the easiest fix would be to change Predef.

odersky commented 5 years ago

For the time being, you can should be able to get around defining this Conversion instance

implied [A <: B, B] widen for Conversion[A, B] {
  def apply(x: A): B = x
}

The worry is that this can give you ambiguities. That's why it's better to change Predef so that <:< coincides with widen.

drdozer commented 5 years ago

yea, I tried various.

implied widen[A <: B, B] for Conversion[A, B] = identity : (A => B)

This crashes the compiler for me.

implied widen[A <: B, B] for Conversion[A, B] = identity

This works.

Is there a reason why we can't simply replacey <:< with this widen? Are there some compiler magickry that needs to know about it first?

odersky commented 5 years ago

Awaiting full bootstrap for resolution

SethTisue commented 1 year ago

current syntax:

scala> given [A <: B, B]: Conversion[A, B] = identity
def given_Conversion_A_B[A <: B, B]: Conversion[A, B]

scala> summon[Conversion[String, String]]
val res2: Conversion[String, String] = <function1>

We're now well past full bootstrap. Regardless, we haven't ventured to change Predef yet. But I imagine it would possible to add this to scala3-library and this needn't be considered "stat:blocked" anymore?