[英]Scala type parameter vs. type field when extending higher kinded type
[英]Ignore a higher kinded type parameter when extending a class
上课
class Foo[F[_]]
我想实现丰富特质
trait EnrichedFoo extends Foo[_]
但这会导致输入错误:
error: _$1 takes no type parameters, expected: one
trait EnrichedFoo extends Foo[_]
^
运行示例: https : //scalafiddle.io/sf/CkMwDSi/0
编辑:
为什么? 我想拥有一种适用于任何param值的扩充功能,因此我可以编写如下内容:
class MyFoo extends Foo[List] with EnrichedFoo
我必须扩展Foo
因为我希望能够覆盖其某些方法,因此EnrichedFoo
不能是自类型(如果有帮助的话)。
如果声明以下内容:
trait Foo {
type HK[F]
}
trait EnrichedFoo1 extends Foo {
def enrich1 : Unit = ???
}
trait EnrichedFoo2 extends Foo {
def enrich2 : Unit = ???
}
abstract class FooClass[HK0[_]] extends Foo {
type HK[F] = HK0[F]
}
然后用户可以执行以下操作:
class MyFoo1 extends FooClass[List] with EnrichedFoo1
class MyFoo2 extends FooClass[List] with EnrichedFoo2
class MyFoo12 extends FooClass[List] with EnrichedFoo1 with EnrichedFoo2
当您不能使用简单参数时,为什么可以使用更高种类的参数呢? 如果你有
class Bar[A] ...
class Baz extends Bar
它只会对Bar
的类型参数进行普通类型推断,因此等效于class Baz extends Bar[Nothing]
。
我必须扩展Foo,因为我希望能够覆盖其某些方法,因此EnrichedFoo不能是自类型(如果有帮助的话)。
这些方法不能提及F
,或者您甚至无法在EnrichedFoo
写下它们的签名,对吗? 然后为什么不将Foo
分为两部分:
trait FooBase {
// methods not mentioning F
}
trait Foo[F[_]] extends FooBase {
// other methods
}
trait ExtendedFoo extends FooBase { self: Foo[F] forSome { type F[_] } =>
// method overrides
}
您需要在Foo
使用类型构造函数* -> *
传递具体的类/特征,这意味着它接受类型并返回类型,例如List
接受Int
作为类型参数并返回List[Int]
类型。
class Foo[F[_]]
trait EnrichedFoo extends Foo[List]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.