Suppose I have:
class Bounded[A] { type apply[C <: A] = C }
This compiles:
implicitly[Bounded[Any]
This fails:
type Str = Bounded[Any]
... from:
[error] /home/grant/Workspace/scunits/test/src/main/scala/Box.scala:37: type arguments[String] do not conform to type apply type parameter bounds [C <: A] [error] type Str = Bounded[Any]
I tried using abstract types instead of type parameters, with the same result. The only thing I found was to instantiate the type. This compiles:
val boundedAny = new Bounded[Any] type Str2 = boundedAny.apply[String]
Unfortunately, I work with phantom types that do not have run-time instances, often for performance reasons.
Why is Scala creating a compilation error here? Is there any more efficient work?
Thanks for any help.
Update: In addition to the workaround below, I need a way to override types with abstract type constraints. I did it like this:
object Test { class AbstractBounded[A] { type apply[C <: A] <: A class Workaround[C <: A] { type go = apply[C] } } class Bounded[A] extends AbstractBounded[A] { type apply[C <: A] = C } type Str = Bounded[Any]
types scala
Grant
source share