Detecting Empty Lists
Moderator: Martin
Detecting Empty Lists
The isEmpty() function does not distinguish between a list with zero elements and a null variable. What's the best way to tell them apart?
Re: Detecting Empty Lists
You can check for null:
list == null
or check if list is not null:
list != null
list == null
or check if list is not null:
list != null
Re: Detecting Empty Lists
A further note on this: isEmpty() returns true for null (undefined) variables, empty strings ("") and empty lists ([]), but *not* for empty maps ({}).
The isEmpty() behavior is thus inconsistent, and there is no way to determine if a map is defined but empty.
The isEmpty() behavior is thus inconsistent, and there is no way to determine if a map is defined but empty.
Re: Detecting Empty Lists
Also, even though you can treat a map as a string in many functions (substring, for example), the test
aMap == "{}"
returns false.
aMap == "{}"
returns false.
Re: Detecting Empty Lists
One more thing:
x = newMap();
matches(x, '\\{\\}')
returns true.
x = newMap();
matches(x, '\\{\\}')
returns true.
Re: Detecting Empty Lists
isEmpty does not exist for maps yet so map is converted to a string when the function is called. This is also the case for other method calls. I will add a new isEmpty function specifically for maps.
"{}" is not valid since it will try to evaluate the expression inside the braces and fails. A comparison newMap()=='{}' should work since the curly braces are not interpreted within single quotes.
"{}" is not valid since it will try to evaluate the expression inside the braces and fails. A comparison newMap()=='{}' should work since the curly braces are not interpreted within single quotes.