5

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?

acjay
  • 34,571
  • 6
  • 57
  • 100
  • This is one of the reasons to avoid `Future`. Use a better type that can suspend side effects, and can be manipulated and composed as pure values. There are many options: scalaz Task, monix Task, fs2 Task, cats IO, etc. – Ziyang Liu Sep 19 '17 at 21:08
  • This is unhelpful, but you may be interested to know that dotty will remove this conversion: http://dotty.epfl.ch/docs/reference/dropped/auto-apply.html. None of the linter tools I've used check for this, as far as I know. – Joe K Sep 19 '17 at 21:22
  • @JoeK That's actually great news :) – acjay Sep 19 '17 at 22:09
  • Conversely, `warning: Eta-expansion of zero-argument method values is deprecated. Did you intend to write f()?` – som-snytt Sep 19 '17 at 22:13
  • @som-snytt Ah, I see that was added in 2.12 (https://issues.scala-lang.org/browse/SI-7187). I'm still on 2.11. – acjay Sep 19 '17 at 23:15
  • @som-snytt Damn. The problem is "solved" by killing automatic eta expansion for nullary mthods, rather than not auto-calling them. Annoyingly, that kills the symmetry with methods with more than zero arguments. I get it though, that it would have been a controversial change to adopt Dotty's behavior, and it least this fixes the gotcha in the short run. I'm glad Dotty to what seems to be a cleaner route. – acjay Sep 20 '17 at 22:01

1 Answers1

3

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.

acjay
  • 34,571
  • 6
  • 57
  • 100