Skip to content

Instantly share code, notes, and snippets.

@mgwidmann
Last active March 6, 2018 17:33
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 mgwidmann/43ea07f69d05ccfddd0e8579be748393 to your computer and use it in GitHub Desktop.
Save mgwidmann/43ea07f69d05ccfddd0e8579be748393 to your computer and use it in GitHub Desktop.
Scala's violations of the principle of least surprise
{} + "" == "()" // true
pow(2, 31).intValue + 1 > 0 // false
Set(1,2,3).sameElements(Set(3,2,1)) // false
Some(null).get // No exception :(, Some(null) should result in a None IMO
// Option objects might be null!!!
def safeOption(shouldBeSafe : Option[String]) : String = {
if (shouldBeSafe.isDefined) {
return "Yep its safe"
} else {
return "No, it was not"
}
}
safeOption(null) // BOOM java.lang.NullPointerException :(
// Somes can hold Nones inside them :(, have to use flatMap to remove
Map("a" -> "b").get("a").map(x => Try(x.toInt).toOption) == Some(None)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment