2

WartRemover's NonUnitStatements requires that statements that aren't returning unit must have an assignment. OK, but sometimes we have to use annoying Java APIs that both mutate and return a value, and we don't actually hardly ever care about the returned value.

So I end up trying this:

val _ = mutateSomething(foo)

But if I have multiple of these, _ is actually a legit val that has been assigned to, so I cannot reassign. Wartremover also rightly will admonish for gratuitous var-usage, so I can't just do var _ =.

I can do the following (need the ; to avoid Scala thinking it is a continue definition unless I add a full newline everytime I do this).

;{val _ = mutateSomething(foo)}

Is there a better way?

bbarker
  • 11,636
  • 9
  • 38
  • 62

2 Answers2

7

My general thoughts about linting tools is that you should not jump through hoops to satisfy them.

The point is to make your code better, both with fewer bugs and stylistically. But just assigning to var _ = does not achieve this. I would first be sure I really really don't care about the return value, not even asserting that it is what I expect it to be. If I don't, I would just add a @SuppressWarnings(Array("org.wartremover.warts.NonUnitStatements")) and maybe a comment about why and be done with it.

Scala is somewhat unique in that it's a somewhat opinionated language that also tries to integrate with another not-so-opinionated language. This leads to pain points. There are different philosophies to dealing with this, but I tend to not sweat it, and just be aware of and try to isolate the boundaries.

Joe K
  • 18,204
  • 2
  • 36
  • 58
  • I agree with the sentiment in general that linters shouldn't be followed blindly, but in this particular case, I actually think `var _ = ` lets the reader know, "hey, this is emitting a value and it is being ignored". So I guess I'd like to keep it, but the alternative is stylistically less than idea. – bbarker Jun 22 '17 at 02:17
  • Also, as an aside to another of your points, I actually always thought of Scala itself as being rather unopinionated (though more so than Perl!, haha) - it is the users (and their libraries) who seem to end up being opinionated. But I take your meaning, it is opnionated on the side of safety relative to Java. – bbarker Jun 22 '17 at 02:19
  • I disagree that Scala is opinionated. I think it's one of the least opinionated languages I've used. Perhaps a vocal part of the community has options on how to use the Scala but the language itself rarely if ever gets in the way of writing code however you want. – puhlen Jun 22 '17 at 13:41
  • 1
    Good points - perhaps I should have said it's the community that is opinionated. I more meant that the subset of Scala that is considered "good style" is smaller compared to the language as a whole, in contrast to other languages such as Java. – Joe K Jun 22 '17 at 18:15
5

I'm posting an answer, but the real credit goes to Shane Delmore for pointing this out:

def discard(evaluateForSideEffectOnly: Any): Unit = {
  val _: Any = evaluateForSideEffectOnly
  () //Return unit to prevent warning due to discarding value
}

Alternatively (or see @som-snytt's comments below):

@specialized def discard[A](evaluateForSideEffectOnly: A): Unit = {
  val _: A = evaluateForSideEffectOnly
  () //Return unit to prevent warning due to discarding value
}

Then use it like: discard{ badMutateFun(foo) }.

Unlike the ;{ val _ = ... } solution, it looks way better, and also works in the beginning of a block without needing to alter style (a ; can't come at the start of a block, it must come after a statement).

bbarker
  • 11,636
  • 9
  • 38
  • 62
  • 3
    `implicit class xxx(val u: Unit) /*extends AnyVal*/ { @specialized def apply[A](a: A): Unit = () }` then `() { 42 }`. – som-snytt Jun 23 '17 at 05:11
  • @som-snytt interesting alternative! Is using @specialized with polymorphic functions more performant than just using Any as the parameter type of `apply`? – bbarker Jun 23 '17 at 13:09
  • 1
    The call-by-name param you show seems unnecessary; specialized param avoids boxing. – som-snytt Jun 24 '17 at 05:33