Null object: Difference between revisions
Replace println() with print(); replace output "syntaxhighlight" tag with "pre" tag
(Add Ecstasy example) |
(Replace println() with print(); replace output "syntaxhighlight" tag with "pre" tag) |
||
Line 735:
{
@Inject Console console;
console.
// String s = Null; // <-- compiler error: cannot assign Null to a String type
String? s = Null; // "String?" is shorthand for the union "Nullable|String"
String s2 = "test";
console.
// Int len = s.size; // <-- compiler error: String? does not have a "size" property
Int len = s?.size : 0;
console.
if (String test ?= s)
Line 755:
}
//
s2 = s; // at this point, s is known to be a non-null String
console.print($"s={s}, s2={s2}, (s==s2)={s==s2}");
}
}
</syntaxhighlight>
{{out}}
<pre>
Null value=Null, Null.toString()=Null
s=Null, s2=test, (s==s2)=False
len=0
</pre>
=={{header|Eiffel}}==
|