Prevent inactive 0-ary functions in Scala

I received damage from a production error in which I passed an unclean 0-ary function to a class that mistakenly expected a simple result type.

def impureFunc(): Future[Any] = ??? case class MyService(impureDependency: Future[Any] /* should have been () => Future[Any] */) 

In fact, this made MyService immediately call impureFunc and cache the first result for the program's lifetime, which led to a very subtle error.

Typically, the type system prevents such errors, but because of the possibility of calling 0-ary functions without a list of arguments, the compiler accepted this program.

Obviously, this is a β€œfunction” of Scala, designed to make the code more understandable, but it was a bad way. Is there a way to make this a compiler warning or an enumeration error? In other words, reject the type "Empty application" implicit conversion method ?

+5
source share
1 answer

From the comments here it seems that this behavior is outdated with the warning in 2.12 and should be a mistake in 2.13. So the answer seems to be to use -deprecation -Xfatal-warnings after the upgrade.

+3
source

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


All Articles