Last active
March 6, 2018 17:33
-
-
Save mgwidmann/43ea07f69d05ccfddd0e8579be748393 to your computer and use it in GitHub Desktop.
Scala's violations of the principle of least surprise
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{} + "" == "()" // 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