Null object: Difference between revisions

Added Wren
(Added Wren)
Line 1,997:
<lang wart>(not nil)
</lang>
 
=={{header|Wren}}==
In Wren, it is not technically possible for a variable to have no value at all.
 
If you define a variable without giving it a value, then it is given the special value ''null'' which is the only instance of the Null class and also a keyword.
Similarly, if you define a function but don't give it a return value, then it returns ''null''. In this particular respect, null is similar to ''void'' in C.
 
In boolean expressions, ''null'' is considered to be false whereas (apart from ''false'' itself) all other values (even 0) are considered to be true. For consistency the Null class therefore overrides the ''!'' operator, which it inherits from the Object class, so that ''!null'' returns true.
 
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.
<lang ecmascript>// Declare a variable without giving it an explicit value.
var s
 
// We can now check for nullness in one of these ways.
System.print(s)
System.print(s == null)
System.print(s is Null)
System.print(s.type == Null)
 
// Similarly, if we define this function without giving it a return value.
var f = Fn.new {
System.print("I'm a function with no explicit return value.")
}
 
// And now call it.
var g = f.call()
// We find that the return value is null.
System.print(g)</lang>
 
{{out}}
<pre>
null
true
true
true
I'm a function with no explicit return value.
null
</pre>
 
=={{header|zkl}}==
9,476

edits