Null object: Difference between revisions

Content added Content deleted
Line 730: Line 730:


<syntaxhighlight lang="java">
<syntaxhighlight lang="java">
module NullObject
module NullObject {
{
void run() {
void run()
{
@Inject Console console;
@Inject Console console;
console.print($"Null value={Null}, Null.toString()={Null.toString()}");
console.print($"Null value={Null}, Null.toString()={Null.toString()}");
Line 746: Line 744:
console.print($"len={len}");
console.print($"len={len}");


if (String test ?= s)
if (String test ?= s) {
{
// "s" is still Null in this test, we never get here
// "s" is still Null in this test, we never get here
}
} else {
else
{
s = "a non-null value";
s = "a non-null value";
}
}


// if (String test ?= s){} // <-- compiler error: The expression type is not nullable: "String"
// if (String test ?= s){} // <-- compiler error: The expression type is not nullable: "String"
s2 = s; // at this point, s is known to be a non-null String
s2 = s; // at this point, s is known to be a non-null String
console.print($"s={s}, s2={s2}, (s==s2)={s==s2}");
console.print($"s={s}, s2={s2}, (s==s2)={s==s2}");
}
}
}
}
</syntaxhighlight>
</syntaxhighlight>