Missing values, nulls, and nils. What is what?

I’m a little confused. So what I think about these are:

¢Missing value is when you don’t have any data to store into a variable.

¢Null is nothing as a character 00.

¢Nil is nothing without absence.

Am I wrong?

Hello Dylan.

I have no experience with nil in plain AppleScript.

Missing value: the variable is defined but is missing a value, (which is actually a class).

Null: you can view null as null, as a value which would be something like memory location 0 (which is defined to contain 0), if this were C, -I just view null as null.
Edit:The reason for having a null value, is to have a value that is guaranteed not to show up in some other context, - an unique value that can’t be mistaken for some other. I think that nil would be a synonym for null but I’m not totally sure.

I’d like to add undefined to the debacle.
undefined is not a class but a state but different from missing a value as missing a value is a variable which is defined to be of the class of variables that are missing a value, and undefined, is more at the level below, where nothing is defined about that variable.



set a to 10
log a ”> 10
set a to undefVar()
log a ”> "Error the variable a is undefined"

on undefVar()
	return
end undefVar

I don’t know if this helps.

Hi, Dylan.

‘missing value’ is an AppleScript placeholder token you can use as a temporary value for a variable, list item, record property, etc. or when you’re not able to return a value in a situation where one is expected.

There’s a compilable ‘null’ keyword in AppleScript, but it’s not advertised as part of the language, so it’s best not to use it. I believe the word itself is etymologically related to “nil”.

“Nil” is the English contraction of the Latin “nihil”, meaning “nothing”. It has no meaning in AppleScript. (There’s a witticism to be had in there somewhere, but I can’t put it together this early in the morning. :wink: )

Hello.

I opened AppleScript the Definitive Guide By Matt Neuberg, and to my astonishment I see that null is implemented the same way as the missing value. They can both be viewed as place holders, or classes, or constants that are not holding any values.

That leaves us with 4 states a variable can hold:

  1. undefined: generates a run time error.

  2. missing value, (absolutely no value at all),

  3. null: a value returned which isn’t any possible returned normal value often used like you would use “” to signal nothing or an empty value.

  4. Any normal value. (I’d like to place the empty string together with null, which designates the state to be empty. But that would be as incorrect here as in C, but it would have been great to use a constant like EMPTY for that.

In reality null and missing value can be perceived as the same. And AS uses missing value as opposed to null.
Edit
Giving this a second thought; null might be bad to use, since it is nothing like NULL in C, where it is a reference to a memory location which are guaranteed not to be used by anything else. But there is a potential for leveraging upon having two such values to when programming finite state machines for instance. Then you could use the different values to identify different states, and thereby simplify the logic by avoiding extranous control variables.

-I think I just saw the light with a problem I have! :smiley: Thanks Dylan!


set mylist to {"apples", "pears", "bananas"}
set colorList to {"red", "green", "yellow"}
set newList to {}
repeat with i from 1 to (count mylist)
	set item i of mylist to item i of colorList & " " & item i of mylist --contents of item i of colorList
end repeat

set item 2 of mylist to missing value ” (Same result with null below)
log mylist -- (*red apples, missing value, yellow bananas*)
set mylist to mylist's text ” assigns every item of that class in myList back to myList
log mylist -- *red apples, yellow bananas*)


An interesting use of missing value is when we need to define some lists as properties to fasten their treatment.
Most of the time, we don’t need to store the value of these lists and even it would be bad practice to do that allowing curious persons to grab ‘confidential’ infos.
So, when I leave such scripts, I reset the properties to missing value.

Yvan KOENIG (VALLAURIS, France) mercredi 4 août 2010 16:33:17

Hello.

It is often used as a value in a property to signal that the property is unset, for example the first time the user runs a script.


property homePath : missing value

if homePath is missing value then 
	set homePath to (path to home folder as alias )
end if 

It is interesting :slight_smile: we have missing value that is a class that signals missing value which is no value, but not undefined.
Then we have null, which we can use to say that the variable has been used, but the result returned no value.
Im not sure if there are any differences of the two, but I will prefer to use the word null, when my handlers are communication with my script, test for missing values from applications like Finder, null is class i believe we are given for that purpose. (But it is kind of too easy to just return “”) :slight_smile:

It should be more efficient to use null though, as I believe AppleScript won’t allocate any storage yet.

I haven’t timed this but
(This allocates as a matter of fact 100 elements of missing value)


property mylist : {}
repeat 100 times
	set end of mylist to missing value
end repeat

Should be faster than


property mylist : {}
repeat 100 times
	set end of mylist to ""
end repeat

The opposite appeared to be true assigning “” instead of missing value was 0.000000571666667 faster per assignment. I iterated over both cases in loops of 10000 iterations 6 times for each construct, I recompiled in between every run. I did the calculation by adding up the results for each case, before subtracting the smallest from the largest of the two cases then dividing by 6 and finally dividing by 100000.

I conclude by this that “” is another kind of special value/constant in AppleScript.

As a matter of fact this isn’t so, I tested assigning long strings afterwards and they got the same result as the previous one, surely because it is the same string which are referenced, but assigning by a number took also the same time as assigning a string. -So there is some extra work concerning the missing value, if i dared guess I’d say that the memory manager where being called upon. -Which may be more efficient in the long run, that is if it is so.
The second guess is that the missing value points to some special memory location, which may global, which means that AppleScript has to go to the memory manager, and fetch the address from there every time. A third guess is that the mechanism for setting a class explicitly through a constant, takes longer time than automatically assigning the value of some “typeless” variable, but the class must be set somewhere then too, since we can use the class of operator, but in this case that may have happened compile time so that the variable set was prepared with the class information.

Edit:The null property appear to use the same time as the missing value.

Regarding annulling properties:
It is a good idea to get rid of especially large data structures by assigning the property that holds the reference to it to something else, in order to free memory (an unreferenced data structure should be sent to the garbage collector), as well as to avoid prying eyes.

Woah, my post generated a lot of replies! :lol:

Well, now this helps me understand almost every coding language.

Now that’s helpful! But, does anyone know nil a little better?

nil is used in Objective-C, it’s not defined in the AppleScript language.

Hello Dylan,

I found this: in the The Objective C’s beginners guide

NULL here is the same null as I talked about in context with C earlier.

Mostly noise, it must be said. :slight_smile:

Hello.
That is correct Nigel. But it was interesting, and I feel that I understand everything maybe a little bit better.
I guessed three different guesses as to why the assignment of a missing value takes longer time than a regular variable, both string (immutable/reference) or a “standard” one typeless/by value were faster to assign to a list element. :rolleyes:

Should you happen to know the answer to the question? -It is of no practical importance, just sheer curiosity.

I don’t know what any of this means. ‘Missing value’ isn’t a variable, it’s a value.

set fref to (open for access file ((path to desktop as text) & "Missing value.data") with write permission)
try
	set eof fref to 0
	write missing value to fref
	set t to (read fref from 1 as text)
end try
close access fref

t --> "msng" (4-byte text-based token)

And it doesn’t take any longer to assign one kind of item to a list position than any other. It’s simply a matter of adding or changing a pointer. What makes the difference ” when there is any ” is the time it takes to create the item itself to put in the list.


-- Create 5000 instances of 'missing value' and assign each to a position in a list.
set l to {}
repeat 5000 times
	set end of l to missing value
end repeat

-- Faster: create just 1 instance of 'missing value' and assign it to 5000 positions in a list.
set l to {}
set mv to missing value
repeat 5000 times
	set end of l to mv
end repeat

I can’t find any consistent difference between the time it takes to assign missing values to a list and the time it takes to assign empty texts.

Hello.

I found a consistent difference when measured the time 6 times for each of the two cases, I also did the same test with null. -And found the same consistency. I did however use the constant. which I hadn’t assigned to a variable, and thats maybe were the dog is buried. But “” is also a constant!

This is of no importance really, but it seems like it for some odd reason, that the constants missing value and null takes some (very tiny amount of) time longer to allocate into a variable than “”.

:smiley: - I really have enough todo !

Maybe the missing value is a place holder for a memory location, maybe that memory location is stored somewhere in the system, I don’t know, and I regret that I started bother about it. :slight_smile:

But it do take a tiny bit longer time to assign one of those constant/values than “”. :slight_smile:

I’ve never wasted time to think about saving 10 nanoseconds by writing missing value instead of empty string.
It’s more important to write reliable code by assigning the proper “empty” value depending on the class of the variable

boolean : false
integer : 0
real : 0.0
string : “”
list / record : {}
object/element : missing value

Hello Stefan.

I wasn’t really that much interested about the nano seconds, as what happens within those nano seconds. The answer I opted for was that “assigning a missing value to a property or variable hands over the previous contents to the memory manager”, or : the reason that the missing value assignment takes longer time, is because the address that the missing value constant points to is within the program segment when your script is run".

Sometimes it is appropriate to assign a value of a different class, since all variables are “typeless” any way, I don’t see how this can lead to unreliable code, if one know how to do it, and about any pitfalls one then has to check for.

The super example is when you have a list of strings or text, and assign “missing value” to elements you want to delete and then assign the list’s text to the list, effectively deleting any elements that is not of the text class -the elements that contain missing value. -So there are good uses as well. -I think we both mean the same thing about this. As of course have the same opinion as you generally
And now I want waste more hours on nano-seconds. :smiley: