Null object: Difference between revisions

No edit summary
(8 intermediate revisions by 4 users not shown)
Line 730:
 
<syntaxhighlight lang="java">
module NullObject {
void run() {
void run()
{
@Inject Console console;
console.print($"Null value={Null}, Null.toString()={Null.toString()}");
Line 740 ⟶ 738:
String? s = Null; // "String?" is shorthand for the union "Nullable|String"
String s2 = "test";
console.print($"{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.print($"{len={len}");
 
if (String test ?= s) {
{
// "s" is still Null in this test, we never get here
} else }{
else
{
s = "a non-null value";
}
 
// 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
console.print($"{s={s}, {s2={s2}, ({s==s2)={s==s2}");
}
}
}
</syntaxhighlight>
 
Line 765 ⟶ 760:
<pre>
Null value=Null, Null.toString()=Null
s=Null, s2=test, (s==s2)=False
len=0
s=a non-null value, s2=a non-null value, (s==s2)=True
</pre>
 
Line 809 ⟶ 804:
nil</syntaxhighlight>
 
=={{header|EMalLEMal}}==
<syntaxhighlight lang="emal">
^|
Line 819 ⟶ 814:
int b # defaults to 0
void c # only one allowed value: null
writeLine("nullable var equals withto not nullable int: " + (a == b)) # allowed, false
writeLine("type of a equals to Void data type: " + (generic!a == void)) # true:^| if the data type of a is void we are sure that a is null |^
writeLine("type of a equals to Void data type: " + (generic!a == void)) # true
writeLine("integer value " + b + " equals to null: " + (b == null)) # allowed, always false
writeLine("a void value equals to null: " + (c == null)) # always true</syntaxhighlight>
</syntaxhighlight>
{{out}}
<pre>
nullable var equals withto not nullable int: ⊥
type of a equals to Void data type: ⊤
integer value 0 equals to null: ⊥
Line 939 ⟶ 934:
</pre>
 
=={{header|GDScript}}==
Godot has a null value. Here is an example of dealing with null.
<syntaxhighlight lang="gdscript">
extends Node2D
 
func _ready() -> void:
var empty : Object
var not_empty = Object.new()
# Compare with null.
if empty == null:
print("empty is null")
else:
print("empty is not null")
# C-like comparation.
if not_empty:
print("not_empty is not null")
else:
print("not_empty is null")
return
</syntaxhighlight>
{{out}}
<pre>
empty is null
not_empty is not null
</pre>
 
=={{header|Go}}==
Line 1,168 ⟶ 1,189:
 
=={{header|langur}}==
Null can be compared for directly, using equality operators, or can be checked with the isNull() function. Operators ending with a ? mark propagate null. A null in an expression test is a non-truthy result.
 
{{works with|langur|0.10}}
Prior to 0.10, multi-variable declaration/assignment would use parentheses around variable names and values.
 
<syntaxhighlight lang="langur">val .x, .y = true, null
Line 2,320 ⟶ 2,338:
 
It is always easy to test for nullness either by querying a variable's type or checking the value of a boolean expression involving a potentially null variable.
<syntaxhighlight lang="ecmascriptwren">// Declare a variable without giving it an explicit value.
var s
 
885

edits