Scala 中的 ^^^ 运算符是什么?
What is the ^^^ operator in Scala?
我在一些 Scala 代码中看到 ^^^
的用法,但不理解它的用法,也看不到任何关于它的文档。另外我很确定它不是来自任何外部库,但可能是这样,也许吧?我知道 ^^
是 .map()
的运算符,但不知道当你有第三个 ^.
时是否有相似之处
使用示例:
case object TypeFoo extends Type {
override def toString() = "Foo"
}
case object TypeBar extends Type {
override def toString() = "Bar"
}
def repType= (
"Foo" ^^^ TypeFoo
| "Bar" ^^^ TypeBar
)
根据我的理解,它可能是“由”定义的,但我真的不确定,因此我的问题。
^^^
方法的 Javadoc 说:
A parser combinator that changes a successful result into the specified value.
p ^^^ v
succeeds if p
succeeds; discards its result, and returns v
instead.
@param v
The new result for the
parser, evaluated at most once (if p
succeeds), not evaluated at all
if p
fails.
@return
a parser that has the same behaviour as the
current parser, but whose successful result is v
换句话说,"Foo" ^^^ TypeFoo
只是"Foo" ^^ (_ => TypeFoo)
的shorthand。
我在一些 Scala 代码中看到 ^^^
的用法,但不理解它的用法,也看不到任何关于它的文档。另外我很确定它不是来自任何外部库,但可能是这样,也许吧?我知道 ^^
是 .map()
的运算符,但不知道当你有第三个 ^.
使用示例:
case object TypeFoo extends Type {
override def toString() = "Foo"
}
case object TypeBar extends Type {
override def toString() = "Bar"
}
def repType= (
"Foo" ^^^ TypeFoo
| "Bar" ^^^ TypeBar
)
根据我的理解,它可能是“由”定义的,但我真的不确定,因此我的问题。
^^^
方法的 Javadoc 说:
A parser combinator that changes a successful result into the specified value.
p ^^^ v
succeeds ifp
succeeds; discards its result, and returnsv
instead.
@param v
The new result for the parser, evaluated at most once (ifp
succeeds), not evaluated at all ifp
fails.
@return
a parser that has the same behaviour as the current parser, but whose successful result isv
换句话说,"Foo" ^^^ TypeFoo
只是"Foo" ^^ (_ => TypeFoo)
的shorthand。