This file contains 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
var x: String = null //error! | |
var x: String? = null // ok! |
This file contains 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
String x = null; // error! | |
String? x = null; // ok! |
This file contains 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
val l = b?.length() ?: -1; | |
// which is a shorthand notation for | |
val l: Int = if (b != null) b.length() else -1 |
This file contains 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
String? x = "Some String" | |
Integer l = x.size // compilation error | |
Integer? l = x.size // compilation error | |
Integer? l = x?.size // this works. |
This file contains 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
var b: String? = "abc" | |
b.length() // error: variable 'b' can be null | |
b?.length() // ok - returns value of type Int? |
This file contains 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
data Maybe a = Nothing | Just a. |
This file contains 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
type 'a option = None | Some of 'a |
This file contains 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
sealed abstract class Option[+A] extends Product with Serializable { | |
... | |
} | |
final case class Some[+A](x: A) extends Option[A] { | |
def isEmpty = false | |
def get = x | |
} | |
case object None extends Option[Nothing] { | |
def isEmpty = true |
This file contains 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
//proper way to wrap calls to java apis | |
val map = new java.util.HashMap[String, String]() | |
map.put("key1", "value1") | |
val maybeValue1 = Option(map.get("key1")) //Some("value1") | |
val maybeValue2 = Option(map.get("key2")) //None | |
//equivalent of "if(x != null && condition(x))" | |
val optionX: Option[T] = Option(x) | |
optionX.exists(condition) |
This file contains 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
#simple example | |
=> (if nil "yes" "no") | |
"no" | |
# "elvis" | |
=> (or nil "Some value") | |
"Some value" | |
# if-let construct | |
=> (if-let [a-value (:a a-map)] |
OlderNewer