What is the difference between 'null' and 'None' in Scala?
Instruction: Explain the conceptual and practical differences between 'null' and 'None' in Scala.
Updated
null is a null reference. None is an ordinary singleton value representing the empty case of Option. They are not interchangeable: a method returning Option[A] should return None for absence, not a null option.
@main def absenceExample(): Unit = {
val nullable: String = null
val absent: Option[String] = Option(nullable)
val presentNull: Option[String] = Some(nullable)
assert(absent == None)
assert(absent.getOrElse("fallback") == "fallback")
assert(presentNull.isDefined && presentNull.get == null)
assert(None != null)
}
Use Option(value) when converting a nullable value at an interoperability boundary. Calling Some(value) does not filter null. Consume options through mapping, matching or a deliberate fallback; .get on None throws. Option makes absence explicit but does not force the caller to handle it safely or validate nested fields. Option semantics
The example uses Scala's default null mode. Scala 3's optional explicit-nulls mode separates nullable reference types from their non-null counterparts and changes which assignments type-check. It is an additional static checking mode, not a change that turns null into None. Explicit nulls