Scala推断_

时间:2014-09-18 13:40:39

标签: scala type-inference

我是scala的新手(fxnl编程),虽然我把_放在上下文中的几个地方

list.map(_ * 1) 

我无法完全理解这句话

val story = (catch _) andThen (eat _)

虽然我可以通过调用来推断

story(new Cat, new Bird)

下划线作为参数位置的占位符,我想理解它背后的概念。

1 个答案:

答案 0 :(得分:4)

声明

val story = (catch _) andThen (eat _)

不正确 - 因为catch是关键字。

但这是正确的:

scala> def caught(x:Int) = x + 8
caught: (x: Int)Int

scala> def eat(x:Int) = x + 3
eat: (x: Int)Int

scala> val story = (caught _) andThen (eat _) // story is function.
story: Int => Int = <function1>

scala> story(90) // You put 90 - parameter for caught (first _). It returns 90 + 8 and put it to eat (second _). eat function return 98 + 3
res0: Int = 101