Difference between revisions of "Null"

From MiniScript Wiki
Jump to navigation Jump to search
(Created page with "<c>null</c> is a keyword that represents the null value in MiniScript. Category:Language Category:Keywords {{stub}}")
 
 
(One intermediate revision by the same user not shown)
Line 1: Line 1:
 
<c>null</c> is a [[:Category:Keywords|keyword]] that represents the null value in MiniScript.
 
<c>null</c> is a [[:Category:Keywords|keyword]] that represents the null value in MiniScript.
 +
 +
== null is treated as false ==
 +
 +
<c>null</c> is considered <c>false</c> in [[if]] or [[while]] conditions, as well as in logical operations [[and]], [[or]], and [[not]].  For example, <c>1 and null</c> evaluates to 0, while <c>not null</c> evaluates to 1.
 +
 +
== Numeric arithmetic with null is null ==
 +
 +
When <c>null</c> is involved in any numeric expression, the result is <c>null</c>.  For example, <c>null + 42</c> is <c>null</c>.
 +
 +
== null converts to an empty string ==
 +
 +
When <c>null</c> is added to a string, or explicitly converted to a string with [[str]], it behaves the same as the empty string, <c>""</c>.
 +
 +
The exception to this is the standard [[print]] function; <c>print null</c> will actually print the string “null” rather than printing an empty string.
 +
 +
== null coalescing ==
 +
 +
There is no built-in [https://en.wikipedia.org/wiki/Null_coalescing_operator null-coalescing operator] (i.e., an operator to select the first non-null value of two operands) in MiniScript.  However, one could define a function for this purpose as follows.
 +
 +
<ms>ifNull = function(possiblyNullValue, defaultValue)
 +
  if possiblyNullValue == null then return defaultValue else return possiblyNullValue
 +
end function</ms>
 +
 +
Once defined, then <c>print ifNull(x, "nothing")</c> would print “nothing” when x is null, or the value of x otherwise.
  
 
[[Category:Language]]
 
[[Category:Language]]
 
[[Category:Keywords]]
 
[[Category:Keywords]]
 
+
[[Category:Data Types]]
{{stub}}
 

Latest revision as of 21:20, 15 February 2024

null is a keyword that represents the null value in MiniScript.

null is treated as false

null is considered false in if or while conditions, as well as in logical operations and, or, and not. For example, 1 and null evaluates to 0, while not null evaluates to 1.

Numeric arithmetic with null is null

When null is involved in any numeric expression, the result is null. For example, null + 42 is null.

null converts to an empty string

When null is added to a string, or explicitly converted to a string with str, it behaves the same as the empty string, "".

The exception to this is the standard print function; print null will actually print the string “null” rather than printing an empty string.

null coalescing

There is no built-in null-coalescing operator (i.e., an operator to select the first non-null value of two operands) in MiniScript. However, one could define a function for this purpose as follows.

ifNull = function(possiblyNullValue, defaultValue)
   if possiblyNullValue == null then return defaultValue else return possiblyNullValue
end function

Once defined, then print ifNull(x, "nothing") would print “nothing” when x is null, or the value of x otherwise.