higherKinds 类型别名的编译问题
Compilation issue with type alias of higherKinds
我有一个特点
trait T{
type F[_]
def get[A](f: F[A]): A
}
但是我实现不了
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
// ^
(请注意,如果我投 f.asIntanceOf[A]
但我没有尝试,我认为我应该工作)
我和 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[_] where type _
// required: scala.concurrent.Awaitable[A]
// def get[A](f: F[A]): A = Await.result(f, Duration.Inf)
// ^
谁能给我解释一下这是怎么回事?为什么编译器无法理解 F[A]
正在使用上述类型别名的实际类型?
你可能想写
type F[X] = Id[X]
和
type F[X] = Future[X]
示例:
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
}
我假设您必须等待 Dotty 和对 type-lambda 的完全支持,直到您可以删除双方的冗余参数。
我有一个特点
trait T{
type F[_]
def get[A](f: F[A]): A
}
但是我实现不了
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
// ^
(请注意,如果我投 f.asIntanceOf[A]
但我没有尝试,我认为我应该工作)
我和 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[_] where type _
// required: scala.concurrent.Awaitable[A]
// def get[A](f: F[A]): A = Await.result(f, Duration.Inf)
// ^
谁能给我解释一下这是怎么回事?为什么编译器无法理解 F[A]
正在使用上述类型别名的实际类型?
你可能想写
type F[X] = Id[X]
和
type F[X] = Future[X]
示例:
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
}
我假设您必须等待 Dotty 和对 type-lambda 的完全支持,直到您可以删除双方的冗余参数。