Implicit conversions for members that are types

Given:

implicit class WithRetType[T, U](x: T => U) {
  type Ret = U
}

val foo = (_: Int) * 2
val x: foo.Ret = 3

gives:

  error: type Ret is not a member of Int => Int
val x: foo.Ret = ???
          ^

the following is true:

val foo = (_: Int) * 2
val fooR = new WithRetType(foo)
val x: fooR.Ret = 3

Are implicit conversions allowed to access members that are types?

+4
source share
1 answer

Implicit conversions cannot be used to access type members. Regardless of the implementation details, this can be seen as a logical consequence of two elements in the specification:

  • A path that leads to a path-dependent type must have only stable elements ( Paths in the specification)
  • Implicit conversion is a method / function whose result is always unstable

, : , , , .

+1

All Articles