Skip to content

Instantly share code, notes, and snippets.

@dacr
Last active February 3, 2026 20:26
Show Gist options
  • Select an option

  • Save dacr/04b3aa4c8c47d13330fdd8ea918d71c5 to your computer and use it in GitHub Desktop.

Select an option

Save dacr/04b3aa4c8c47d13330fdd8ea918d71c5 to your computer and use it in GitHub Desktop.
scala3 feature examples - explicit nulls / published by https://github.com/dacr/code-examples-manager #d63219ff-c8cf-46f3-a927-d311db37ab35/1e5eff6e0441a9b04eb26930f5b9d9c2b33ac82
// summary : scala3 feature examples - explicit nulls
// keywords : scala3, tutorial
// publish : gist
// authors : David Crosson
// license : Apache License Version 2.0 (https://www.apache.org/licenses/LICENSE-2.0.txt)
// id : d63219ff-c8cf-46f3-a927-d311db37ab35
// created-on : 2021-04-21T11:45:13+02:00
// managed-by : https://github.com/dacr/code-examples-manager
// run-with : scala-cli $file
//// run-with : scala-cli --scalac-option -Yexplicit-nulls $file
//> using scala "3.4.2"
// using options -Yexplicit-nulls
@main def go():Unit = {
val nullableResult : String | Null = java.lang.System.getenv("PATH")
if (nullableResult != null) {
val result: String = nullableResult // safe because of the previous test :)
println(result)
}
val aresult:String = nullableResult // FAIL when the explicit feature is enabled (-Yexplicit-nulls)
println(aresult)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment