使用类型参数与抽象类型实现类型类
Implementing a typeclass using type parameters versus abstract types
从 开始
我试图在下面的代码片段中并排比较这两种方法:
// We want both ParamaterizedTC and WithAbstractTC (below) to check that
// their B parameter implements AddQuotes
abstract class AddQuotes[A] {
def inQuotes(self: A): String = s"${self.toString}"
}
implicit val intAddQuotes = new AddQuotes[Int] {}
abstract class ParamaterizedTC[A, _B](implicit ev: AddQuotes[_B]) {
type B = _B
def getB(self: A): B
def add1ToB(self: A): String = ev.inQuotes(getB(self)) // TC witness does not need to be at method level
}
abstract class WithAbstractTC[A] private {
// at this point the compiler has not established that type B implements AddQuotes, even if we have created
// this instance via the apply[A, _B] constructor below...
type B
def getB(self: A): B
def add1ToB(self: A)(implicit ev: AddQuotes[B]): String =
ev.inQuotes(getB(self)) // ... so here the typeclass witness has to occur on the method level
}
object WithAbstractTC {
// This constructor checks that B implements AddQuotes
def apply[A, _B: AddQuotes](getB: A => _B): WithAbstractTC[A] = new WithAbstractTC[A] {
type B = _B
def getB(self: A): B = getB(self)
}
// But we could also have a constructor that does not check, so the compiler can never be certain that
// for a given instance of WithAbstractTC, type B implements AddQuotes
def otherConstructor[A, _B](getB: A => _B): WithAbstractTC[A] { type B = _B } = new WithAbstractTC[A] {
type B = _B
def getB(self: A): B = getB(self)
}
}
case class Container[B: AddQuotes]( get: B )
// These are both fine
implicit def containerIsParamaterized[B: AddQuotes]: ParamaterizedTC[Container[B], B] =
new ParamaterizedTC[Container[B], B] { def getB(self: Container[B]): B = self.get }
implicit def containerIsWithAbstract[_B: AddQuotes]: WithAbstractTC[Container[_B]] =
WithAbstractTC[Container[_B], _B](self => self.get)
val contIsParamaterized: ParamaterizedTC[Container[Int], Int] =
implicitly[ParamaterizedTC[Container[Int], Int]]
val contIsWithAbstract: WithAbstractTC[Container[Int]] =
implicitly[WithAbstractTC[Container[Int]]]
implicitly[AddQuotes[contIsParamaterized.B]]
implicitly[AddQuotes[contIsWithAbstract.B]] // This is not fine
我的结论(如果我错了请纠正我)是,如果 public 构造函数中存在类型类见证(如下面的 ParamaterizedTC
所示),那么编译器总是可以确定B
实施 AddQuotes
。而如果这个 witness 被放在类型类伴生对象的构造函数中(比如 WithAbstractTC
),那么它就不能。这在某种程度上改变了基于类型参数的方法与基于抽象类型的方法的用法。
不同之处在于:在 ParametrizedTC
中你有 class 的隐式范围,在 WithAbstractTC
中你没有。但是当你有一个抽象类型时,没有什么能阻止你添加它:
abstract class WithAbstractTC2[A] private {
type B
implicit val ev: AddQuotes[B]
def getB(self: A): B
def add1ToB(self: A): String =
ev.inQuotes(getB(self))
}
def apply[A, _B](getB: A => _B)(implicit _ev: AddQuotes[_B]): WithAbstractTC2[A] = new WithAbstractTC2[A] {
type B = _B
implicit val ev: AddQuotes[B] = _ev
def getB(self: A): B = getB(self)
}
不幸的是,不能工作的是
def apply[A, _B: AddQuotes](getB: A => _B): WithAbstractTC2[A] = new WithAbstractTC2[A] {
type B = _B
implicit val ev: AddQuotes[B] = implicitly[AddQuotes[_B]]
def getB(self: A): B = getB(self)
}
因为它会选择最近范围内的隐式:它试图定义的范围。
implicitly[AddQuotes[contIsWithAbstract.B]]
拒绝编译与single/multiple构造函数/apply
方法或类型parameter/type成员区别无关。您只是到处都失去了类型改进。编译器无法检查您是否丢失了类型改进。您有权向上转换类型并放弃其改进。
如果恢复类型优化,代码会编译
object WithAbstractTC {
def apply[A, _B: AddQuotes](getB: A => _B): WithAbstractTC[A] {type B = _B} =
// ^^^^^^^^^^^^^
new WithAbstractTC[A] {
type B = _B
def getB(self: A): B = getB(self)
}
...
}
implicit def containerIsWithAbstract[_B: AddQuotes]:
WithAbstractTC[Container[_B]] { type B = _B } =
// ^^^^^^^^^^^^^^^
WithAbstractTC[Container[_B], _B](self => self.get)
val contIsWithAbstract: WithAbstractTC[Container[Int]] { type B = Int } =
// ^^^^^^^^^^^^^^^^
shapeless.the[WithAbstractTC[Container[Int]]]
//^^^^^^^^^^^^^
implicitly[AddQuotes[contIsWithAbstract.B]] // compiles
请注意 implicitly
松散类型优化,shapeless.the
是安全版本。
当 implicitly
不够具体时 https://typelevel.org/blog/2014/01/18/implicitly_existential.html
如何通过抽象隐式为 type-member 类型 class 使用 class-level 隐式约束参见 @AlexeyRomanov 的回答。
从
// We want both ParamaterizedTC and WithAbstractTC (below) to check that
// their B parameter implements AddQuotes
abstract class AddQuotes[A] {
def inQuotes(self: A): String = s"${self.toString}"
}
implicit val intAddQuotes = new AddQuotes[Int] {}
abstract class ParamaterizedTC[A, _B](implicit ev: AddQuotes[_B]) {
type B = _B
def getB(self: A): B
def add1ToB(self: A): String = ev.inQuotes(getB(self)) // TC witness does not need to be at method level
}
abstract class WithAbstractTC[A] private {
// at this point the compiler has not established that type B implements AddQuotes, even if we have created
// this instance via the apply[A, _B] constructor below...
type B
def getB(self: A): B
def add1ToB(self: A)(implicit ev: AddQuotes[B]): String =
ev.inQuotes(getB(self)) // ... so here the typeclass witness has to occur on the method level
}
object WithAbstractTC {
// This constructor checks that B implements AddQuotes
def apply[A, _B: AddQuotes](getB: A => _B): WithAbstractTC[A] = new WithAbstractTC[A] {
type B = _B
def getB(self: A): B = getB(self)
}
// But we could also have a constructor that does not check, so the compiler can never be certain that
// for a given instance of WithAbstractTC, type B implements AddQuotes
def otherConstructor[A, _B](getB: A => _B): WithAbstractTC[A] { type B = _B } = new WithAbstractTC[A] {
type B = _B
def getB(self: A): B = getB(self)
}
}
case class Container[B: AddQuotes]( get: B )
// These are both fine
implicit def containerIsParamaterized[B: AddQuotes]: ParamaterizedTC[Container[B], B] =
new ParamaterizedTC[Container[B], B] { def getB(self: Container[B]): B = self.get }
implicit def containerIsWithAbstract[_B: AddQuotes]: WithAbstractTC[Container[_B]] =
WithAbstractTC[Container[_B], _B](self => self.get)
val contIsParamaterized: ParamaterizedTC[Container[Int], Int] =
implicitly[ParamaterizedTC[Container[Int], Int]]
val contIsWithAbstract: WithAbstractTC[Container[Int]] =
implicitly[WithAbstractTC[Container[Int]]]
implicitly[AddQuotes[contIsParamaterized.B]]
implicitly[AddQuotes[contIsWithAbstract.B]] // This is not fine
我的结论(如果我错了请纠正我)是,如果 public 构造函数中存在类型类见证(如下面的 ParamaterizedTC
所示),那么编译器总是可以确定B
实施 AddQuotes
。而如果这个 witness 被放在类型类伴生对象的构造函数中(比如 WithAbstractTC
),那么它就不能。这在某种程度上改变了基于类型参数的方法与基于抽象类型的方法的用法。
不同之处在于:在 ParametrizedTC
中你有 class 的隐式范围,在 WithAbstractTC
中你没有。但是当你有一个抽象类型时,没有什么能阻止你添加它:
abstract class WithAbstractTC2[A] private {
type B
implicit val ev: AddQuotes[B]
def getB(self: A): B
def add1ToB(self: A): String =
ev.inQuotes(getB(self))
}
def apply[A, _B](getB: A => _B)(implicit _ev: AddQuotes[_B]): WithAbstractTC2[A] = new WithAbstractTC2[A] {
type B = _B
implicit val ev: AddQuotes[B] = _ev
def getB(self: A): B = getB(self)
}
不幸的是,不能工作的是
def apply[A, _B: AddQuotes](getB: A => _B): WithAbstractTC2[A] = new WithAbstractTC2[A] {
type B = _B
implicit val ev: AddQuotes[B] = implicitly[AddQuotes[_B]]
def getB(self: A): B = getB(self)
}
因为它会选择最近范围内的隐式:它试图定义的范围。
implicitly[AddQuotes[contIsWithAbstract.B]]
拒绝编译与single/multiple构造函数/apply
方法或类型parameter/type成员区别无关。您只是到处都失去了类型改进。编译器无法检查您是否丢失了类型改进。您有权向上转换类型并放弃其改进。
如果恢复类型优化,代码会编译
object WithAbstractTC {
def apply[A, _B: AddQuotes](getB: A => _B): WithAbstractTC[A] {type B = _B} =
// ^^^^^^^^^^^^^
new WithAbstractTC[A] {
type B = _B
def getB(self: A): B = getB(self)
}
...
}
implicit def containerIsWithAbstract[_B: AddQuotes]:
WithAbstractTC[Container[_B]] { type B = _B } =
// ^^^^^^^^^^^^^^^
WithAbstractTC[Container[_B], _B](self => self.get)
val contIsWithAbstract: WithAbstractTC[Container[Int]] { type B = Int } =
// ^^^^^^^^^^^^^^^^
shapeless.the[WithAbstractTC[Container[Int]]]
//^^^^^^^^^^^^^
implicitly[AddQuotes[contIsWithAbstract.B]] // compiles
请注意 implicitly
松散类型优化,shapeless.the
是安全版本。
当 implicitly
不够具体时 https://typelevel.org/blog/2014/01/18/implicitly_existential.html
如何通过抽象隐式为 type-member 类型 class 使用 class-level 隐式约束参见 @AlexeyRomanov 的回答。