Scala underscore use to simplify syntax of function literals

I have the following code:

var x = Array(1,3,4,4,1,1,3)
var m = Int.MaxValue
x.foreach((x)=>(m = m min x))

I tried to simplify last sentence to:

x.foreach((m = _ min m))

But the interpreter says:

scala>  x.foreach((m = _ min m))     
<console>:8: error: missing parameter type for expanded function ((x$1) => x$1.min(m))
    x.foreach((m = _ min m))
                   ^

I tried to be more explicit about the type:

scala>  x.foreach((m = (_:Int) min m))
<console>:8: error: type mismatch;
found   : (Int) => Int
required: Int
    x.foreach((m = (_:Int) min m))
                           ^

The compiler and I don't understand each other :(

Best regards,

Stan

5
задан Andrey Tyukin 22 February 2019 в 20:48
поделиться