gpt4 book ai didi

scala - 扩展类时忽略更高种类的类型参数

转载 作者:行者123 更新时间:2023-12-01 08:13:36 25 4
gpt4 key购买 nike

正在上课

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

编辑:

为什么?我想要一个适用于任何参数值的扩充,所以我可以这样写:

class MyFoo extends Foo[List] with EnrichedFoo

我必须扩展 Foo 因为我希望能够重写它的一些方法,所以 EnrichedFoo 不能是一个 self 类型(如果它有帮助的话)。

最佳答案

如果您声明以下内容:

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

关于scala - 扩展类时忽略更高种类的类型参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51166477/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com