scala3 icon indicating copy to clipboard operation
scala3 copied to clipboard

Type parameter and implicit conversion not being used

Open Dolu1990 opened this issue 3 years ago • 0 comments

Compiler version

3.1.3 / 3.2.0-RC1

Minimized code

https://scastie.scala-lang.org/73ZtErR0QNGKc7VSiP4XEg

import scala.language.implicitConversions

class Data //Model a data instance

class HardType[T <: Data](t : => T) //Model a Data factory
object HardType{
  implicit def implFactory[T <: Data](t : => T): HardType[T] = new HardType(t)
}

class Fragment[T <: Data](hardType : HardType[T]) //Something which need a Data factory

class Rgb extends Data 

//Test case 1 => OK
Fragment(Rgb())  //Implicit convertion from Data to HardType is used by the compiler, all good

//Test case 2 => KO ?? Compiler can't use the implicit convertion from Data to HardType ?
def cloneOf[T <: Data](that : T) : T = that
Fragment(cloneOf(Rgb())) 

Output

Compiler error

Fragment(cloneOf(Rgb())) //KO
//Found:    Playground.Rgb
//Required: Playground.Data & Playground.HardType[Nothing]
//where:    T is a type variable with constraint <: spinal.core.Data

Expectation

Fragment(cloneOf(Rgb())) should not generate a compilation error, as Fragment(Rgb()) is fine and cloneOf is returning the same type than its parameter. Also it worked in Scala 2.

Dolu1990 avatar Jul 22 '22 10:07 Dolu1990