【发布时间】:2021-03-05 13:59:33
【问题描述】:
有没有办法在 scala 3 中拉皮条? (因为隐式将在 scala 3 中被删除) 那么有没有办法使用“给定和使用”来做到这一点?
在 scala 2 中,我通常会做这样的事情:
implicit class ListOps[T](list: List[T]) {
// just for the sake of example
def myFlatMap(f: T => List[T]): List[T] = {
if (this.list.tail.isEmpty) f(this.list.head)
else f(this.list.head) ++ this.list.tail.myFlatMap(f)
}
}
【问题讨论】:
-
请注意,这种模式通常称为扩展方法,您可以在官方 Dotty 文档中找到它们:dotty.epfl.ch/docs/reference/contextual/extension-methods.html
标签: scala extension-methods implicit scala-3