Reputation: 15529
I have a trait like
trait T{
type F[_]
def get[A](f: F[A]): A
}
But I cannot implement it
type Id[+A] = A // same as shapeless Id
object O extends T{
type F[_] = Id[_]
def get[A](f: F[A]): A = f //
}
// error: type mismatch;
// found : f.type (with underlying type O.F[A])
// required: A
// def get[A](f: F[A]): A = f
// ^
(note I think i should work if I cast f.asIntanceOf[A]
but I didn't try)
I have the same problem with Future:
import scala.concurrent.Await
import scala.concurrent.Future
import scala.concurrent.duration.Duration
object O2 extends T{
type F[_] = Future[_]
def get[A](f: F[A]): A = Awaits.result(f, Duration.Inf)
}
// error: type mismatch;
// found : scala.concurrent.Future[_$1] where type _$1
// required: scala.concurrent.Awaitable[A]
// def get[A](f: F[A]): A = Await.result(f, Duration.Inf)
// ^
Can someone explain to me what's happening? Why the compiler cannot understand the actual type F[A]
is using the above type alias?
Upvotes: 3
Views: 45
Reputation: 44908
You probably wanted to write
type F[X] = Id[X]
and
type F[X] = Future[X]
Example:
trait T { type F[_]; def get[A](f: F[A]): A }
object Foo extends T {
type F[X] = List[X]
def get[A](f: List[A]): A = f.head
}
I assume that you will have to wait for Dotty and full support for type-lambdas until you can drop the redundant argument on both sides.
Upvotes: 4