six*_*abs 2 methods scala arity scala-cats circe
A 和我正在与 circe 一起做一些工作来编码/解码一些 ADT,我们遇到了一些我们根本不了解的功能。circe 文档中给出的示例按预期工作,但在深入研究后 - 不清楚解码示例为何有效,因此我们很难推理如何在需要时进行修改。
功能(来自关于 ADT 的 Circe 示例):
import cats.syntax.functor._
import io.circe.{ Decoder, Encoder }, io.circe.generic.auto._
import io.circe.syntax._
object GenericDerivation {
// Encoder Redacted
implicit val decodeEvent: Decoder[Event] =
List[Decoder[Event]](
Decoder[Foo].widen,
Decoder[Bar].widen,
Decoder[Baz].widen,
Decoder[Qux].widen
).reduceLeft(_ or _)
}
Run Code Online (Sandbox Code Playgroud)
我明白了 - 基本上从这个列表中选择第一个有效的解码器 - 有道理但是(!)
or似乎是一个采用按名称参数的一元函数。类型签名是:
final def or[AA >: A](d: => Decoder[AA]): Decoder[AA]
并且reduceLeft(来自 IterableOnce)需要一个二元函数。类型签名是:
def reduceLeft[B >: A](op: (B, A) => B): B
然后我的脑袋爆炸了。我显然错过了一些东西,无法弄清楚。
该示例绝对适用于转换 ADT。鉴于该or函数似乎没有满足所需的类型,为什么/如何工作reduceLeft?
or是一个参数的方法,但不要忘记this.
decoder1.or(decoder2)(aka decoder1 or decoder2) 是关于decoder1,的二元函数decoder2。
final abstract class Int private extends AnyVal {
...
/** Returns the sum of this value and `x`. */
def +(x: Int): Int
...
}
Run Code Online (Sandbox Code Playgroud)
但你仍然可以添加两个Ints: 1 + 1aka 1.+(1)。
所有方法都有一个比其签名中列出的更多的“参数”,即this.
(所有普通参数都是静态this解析的,动态解析的。)
| 归档时间: |
|
| 查看次数: |
75 次 |
| 最近记录: |