我正在遵循Scala 和方法上的模式匹配和功能组合教程.有这样一个例子:composeandThen
scala> def addUmm(x: String) = x + " umm"
scala> def addAhem(x: String) = x + " ahem"
val ummThenAhem = addAhem(_).compose(addUmm(_))
Run Code Online (Sandbox Code Playgroud)
当我尝试使用它时,我收到一个错误:
<console>:7: error: missing parameter type for expanded function ((x$1) => addAhem(x$1).compose(((x$2) => addUmm(x$2))))
val ummThenAhem = addAhem(_).compose(addUmm(_))
^
<console>:7: error: missing parameter type for expanded function ((x$2) => addUmm(x$2))
val ummThenAhem = addAhem(_).compose(addUmm(_))
^
<console>:7: error: type mismatch;
found : java.lang.String
required: Int
val ummThenAhem = addAhem(_).compose(addUmm(_))
Run Code Online (Sandbox Code Playgroud)
但是,这有效:
val ummThenAhem = …Run Code Online (Sandbox Code Playgroud) 我有两个功能,我正在努力compose:
private def convert(value: String)
: Path = decode[Path](value) match {
private def verify(parsed: Path)
: Path = parsed.os match {
Run Code Online (Sandbox Code Playgroud)
我尝试过如下:
verify compose convert _
Run Code Online (Sandbox Code Playgroud)
编译器抱怨:
[error] Unapplied methods are only converted to functions when a function type is expected.
[error] You can make this conversion explicit by writing `verify _` or `verify(_)` instead of `verify`.
[error] verify compose convert _
[error] ^
[error] one error found
Run Code Online (Sandbox Code Playgroud)
我想完成以下事项:
def process(value: String)
: Path =
verify(convert(value))
Run Code Online (Sandbox Code Playgroud)
我究竟做错了什么?
scala ×2