Gesar
Gesar

Reputation: 399

Type projection alias fails with type bounds

I want a type projection alias using type bounds and higher-kinded types, but it fails unexpectedly. Tried in scala 2.12.8 and 2.13.0

The following works:

object Testing {

  trait One[A] {
    trait Two[B <: A]
  }

  type Test = One[Int]#Two[Int]
}

and the following fails:

object Testing {

  trait One[A] {
    type Two[B <: A] = Altogether[A, B]
  }

  trait Altogether[A, B <: A]

  type Test = One[Int]#Two[Int]
}

with error

type arguments [Int] do not conform to type Two's type parameter bounds [B <: A]

I would expect the second example to compile too.

The following works:

type Test = Altogether[Int, Int]

Can anybody come up with an alternative?

Upvotes: 2

Views: 86

Answers (1)

Krzysztof Atłasik
Krzysztof Atłasik

Reputation: 22595

If you replace trait with type it works as expected:

type One[A] = {
    type Two[B <: A] = Altogether[A, B]
}

trait Altogether[A, B <: A]

type Test = One[Int]#Two[Int]

Tested with Scala 2.13.

Upvotes: 4

Related Questions