Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Prevent 0-ary functions from being called implicitly in Scala

I got nipped by a production bug where I passed an impure 0-ary function to a class that mistakenly expected a a bare result type.

def impureFunc(): Future[Any] = ???

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

Effectively, this made MyService immediately invoke impureFunc and cache the first result for the lifetime of the program, which led to a very subtle bug.

Normally, the type system prevents these sort of bugs, but because of the ability to call 0-ary functions without an argument list, the compiler accepted this program.

Obviously, this is a "feature" of Scala, designed to make code look cleaner, but this was a bad gotcha. Is there any way to make this a compiler warning or a linting error? In other words, disapprove the "Empty application" type of implicit method conversion?

like image 328
acjay Avatar asked Oct 18 '22 05:10

acjay


1 Answers

From the comments here, it appears this behavior was deprecated with a warning in 2.12 and should become an error in 2.13. So it seems the answer is to use -deprecation -Xfatal-warnings after upgrading.

like image 119
acjay Avatar answered Nov 03 '22 04:11

acjay