Skip to content

Instantly share code, notes, and snippets.

@dacr
Last active May 25, 2024 10:20
Show Gist options
  • Save dacr/04b3aa4c8c47d13330fdd8ea918d71c5 to your computer and use it in GitHub Desktop.
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/6711391496fbe9f2df9aa298c009e154e19784a2
// summary : scala3 feature examples - explicit nulls
// keywords : scala3, tutorial
// publish : gist
// authors : David Crosson
// license : Apache NON-AI License Version 2.0 (https://raw.githubusercontent.com/non-ai-licenses/non-ai-licenses/main/NON-AI-APACHE2)
// 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