Skip to content

Instantly share code, notes, and snippets.

@alissapajer
Last active August 29, 2015 13:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alissapajer/9938451 to your computer and use it in GitHub Desktop.
Save alissapajer/9938451 to your computer and use it in GitHub Desktop.
import java.util.concurrent.ConcurrentHashMap
object Why {
val data = new ConcurrentHashMap[Int, String]
def update(int: Int, str: String): Unit = {
data.put(int, str)
}
}
// This compiles! But the return type of ConcurrentHashMap#put for our hash map `data` should be String
// Why does Scala let the `update` function return Unit?
@alissapajer
Copy link
Author

Is there a compiler warning flag for this?

@alissapajer
Copy link
Author

Found it: -Ywarn-value-discard

@alissapajer
Copy link
Author

So it turns out you can solve this problem with an implicit def converting Unit to your own special MyUnit and then using MyUnit everywhere you'd otherwise use Unit

@puffnfresh
Copy link

WartRemover also catches value discarding - pretty much the first thing I put in 🐱

I strongly recommend using -Xfatal-warnings -Ywarn-value-discard!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment