Unable to understand placeholder behavior in scala application

Welcome to Scala version 2.10.2 Type in expressions to have them evaluated. Type :help for more information. scala> val fn = (x:Int) => x+1 fn: Int => Int = <function1> scala> val fn1 = fn _ fn1: () => Int => Int = <function0> scala> val fn2 = fn1 _ fn2: () => () => Int => Int = <function0> 

I don’t understand why the placeholder application (without the suggested type) for the function creates a new curried function with the prefix of the additional argument void.

I was expecting a compiler error or at least a warning.

+2
source share
1 answer

I assume this is due to the principle of uniform access: in REPL val this is an object field, not a local variable. And all non private[this] tags are getter methods.

So your code looks something like this:

 def fn() = (x:Int) => x+1 val fn1 = fn _ // () => fn() 

It works as expected with local variables:

 scala> { | val fn = (x:Int) => x+1 | val fn1 = fn _ | } <console>:10: error: _ must follow method; cannot follow Int => Int val fn1 = fn _ ^ 

Although I can explain why it works this way, I still think that this behavior can be considered a mistake.

+3
source

Source: https://habr.com/ru/post/980401/


All Articles