Compilation Failure Warning: eta null argument extension

When compiling this snippet, the scala compiler generates the following warning:

The Eta extension of the values ​​of a method with a null argument is deprecated. Did you intend to write Main.this.porFiles5 ()? [warn] timerFunc (porFiles5)

This happens when I pass a function to another for a simple time. The timer function accepts, without parameters, a function that returns one in this line: timerFunc(porFiles5) . Is this warning necessary? What would be the idiomatic way to avoid this?

 package example import java.nio.file._ import scala.collection.JavaConverters._ import java.time._ import scala.collection.immutable._ object Main extends App { val dir = FileSystems.getDefault.getPath("C:\\tmp\\testExtract") def timerFunc (func:()=>Unit ) = { val start = System.currentTimeMillis() timeNow() func() val finish = System.currentTimeMillis() timeNow() println((finish - start) / 1000.0 + " secs.") println("==================") } def porFiles5(): Unit = { val porFiles5 = Files.walk(dir).count() println(s"You have $porFiles5 por5 files.") } def timeNow(): Unit = { println(LocalTime.now) } timeNow() timerFunc(porFiles5) timeNow() } 
+5
source share
1 answer

porFiles5 not a function. This is a method that is something completely different in Scala.

If you have a method, but you need a function, you can use the η extension to raise the method into a function, for example:

 someList.foreach(println _) 

Scala will also perform η-extension automatically in some cases, if it is completely clear from the context what you mean, for example:

 someList.foreach(println) 

However, there is ambiguity for parameterless methods, since Scala allows calling without parameters without an argument list, that is, a method defined with an empty parameter list can be called without any argument list:

 def foo() = ??? foo // normally, you would have to say foo() 

Now, in your case, there is ambiguity: do you want to call porFiles5 or do you mean η-extend it? At the moment, Scala arbitrarily eliminates this situation and performs the η extension, but in future versions this will be a mistake, and you will have to explicitly perform the η extension.

So, to get rid of the warning, just use the explicit η extension instead of the implicit η extension:

 timerFunc(porFiles5 _) 
+10
source

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


All Articles