Skip to content

Instantly share code, notes, and snippets.

@mgwidmann mgwidmann/wat.scala
Last active Mar 6, 2018

Embed
What would you like to do?
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
You can’t perform that action at this time.