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.printlnprint($"Null value={Null}, Null.toString()={Null.toString()}");
 
// 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.printlnprint($"s={s}, s2={s2}, (s==s2)={s==s2}");
 
// Int len = s.size; // <-- compiler error: String? does not have a "size" property
Int len = s?.size : 0;
console.printlnprint($"len={len}");
 
if (String test ?= s)
Line 755:
}
 
// s2if (String test ?= s; ){} // <-- compiler error: aThe "String?"expression cannottype beis assignednot to anullable: "String"
s2 = s; // at this point, s is known to be a non-null String
if (String test ?= s)
console.print($"s={s}, s2={s2}, (s==s2)={s==s2}");
{
// the variable "test" is definitely assigned here
console.println($"test={test}");
}
else
{
// s2 = test; // <-- compiler error: "test" is not definitely assigned
}
}
}
</syntaxhighlight>
 
{{out}}
Output:
<pre>
<syntaxhighlight>
Null value=Null, Null.toString()=Null
s=Null, s2=test, (s==s2)=False
len=0
tests=a non-null value, s2=a non-null value, (s==s2)=True
</pre>
</syntaxhighlight>
 
=={{header|Eiffel}}==
162

edits