Null object: Difference between revisions
→{{header|langur}}
(Add Ecstasy example) |
Langurmonkey (talk | contribs) |
||
(11 intermediate revisions by 5 users not shown) | |||
Line 730:
<syntaxhighlight lang="java">
module NullObject {
void run() {
@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) {
// "s" is still Null in this test, we never get here
} else
s = "a non-null value";
// s2 = s; // <-- compiler error: a "String?" cannot be assigned to a "String"▼
console.println($"test={test}");▼
// s2 = test; // <-- compiler error: "test" is not definitely assigned▼
}
▲
▲
}
}
</syntaxhighlight>
{{out}}
<pre>
<syntaxhighlight>▼
Null value=Null, Null.toString()=Null
s=Null, s2=test,
len=0
</pre>
</syntaxhighlight>▼
=={{header|Eiffel}}==
Line 815 ⟶ 803:
<syntaxhighlight lang="elixir">iex(3)> if nil, do: "not execute"
nil</syntaxhighlight>
=={{header|EMal}}==
^|
| EMal has the Variable type (and its keyword var) that is the nullable universal supertype.
| EMal has the Void type (and its keyword void) that holds only one value: null.
| EMal has not nullable types (logic, int, real, text, blob), but null equality is always allowed.
|^
var a # defaults to null
int b # defaults to 0
void c # only one allowed value: null
writeLine("nullable var equals to not nullable int: " + (a == b)) # allowed, false
^| 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>
{{out}}
<pre>
nullable var equals to not nullable int: ⊥
type of a equals to Void data type: ⊤
integer value 0 equals to null: ⊥
a void value equals to null: ⊤
</pre>
=={{header|Erlang}}==
Line 923 ⟶ 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,152 ⟶ 1,189:
=={{header|langur}}==
Null can be compared for directly, using equality operators
<syntaxhighlight lang="langur">val .x, .y = true, null
Line 1,454 ⟶ 1,488:
=={{header|MAXScript}}==
<syntaxhighlight lang="maxscript">if obj == undefined then print "Obj is undefined"</syntaxhighlight>
=={{header|min}}==
<syntaxhighlight lang="min">null null? puts!</syntaxhighlight>
{{out}}
<pre>true</pre>
=={{header|Modula-3}}==
Line 2,299 ⟶ 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="
var s
|