Page 1 of 1

nil - desperandum

Posted: 02 May 2014 10:31
by DavidNewton
Having written a few "quick and dirty" plugins for my own use I made the (possibly rash) decision to try to understand Lua. It didn't take long for me to hit the wall with nil (although I have temporarily ignored my unease in order to make some progress). I have searched some of the Lua community forums with no success and although a side topic for Family Historian I am hoping for some illumination from this forum

According to the Lua reference manual, and I quote

"Nil is the type of the value nil whose main property is to be different from any other value; it usually represents the absence of a useful value."

In the middle of a technical reference manual this seems incredibly vague. There are two undefined adjectives "main" and "useful". The first one of which implies that nil has other properties which we are not discussing, and the second one suggests that nil is usually a useless value but sometimes it is useful.
Reading on: "both nil and false make a condition false". So an expression whose value is nil is to be regarded to have the same property as boolean false? Doesn't this contradict the definition of the value type nil?

Any unassigned variable is (presumably temporarily) assigned the value nil, however, if you attempt to (directly) assign the value nil to a variable then this is a sign that you are done with the variable and it is deleted. On the other hand you can assign a nil value to a variable as the return value of a function and that does not delete the variable.

Further uniqueness of nil is that you amongst all value types it is the only one that you cannot use in a table index or table field.

All of these exception properties of nil suggest: nil is not a real value type, it is an imaginary value type invented to solve some of the problems which arise when you are allowed to use dynamically typed and uninitialised variables. I would feel a bit better if the definition of the nil type included the word type immediately before the semi-colon.

OK rant over.

Can somebody please point me to a discussion of the nil type which makes more sense.

David

Re: nil - desperandum

Posted: 02 May 2014 10:47
by Jane
I am not quite sure what your are having a problem with. nil in most cases means a variable has not been initialised, or has been initialised with a nil value. The latter is useful when dealing with the variable scope options on local variables.

So for example you might do

Code: Select all

local q 
for i = 1,50 do
    print (i)
    q = i
end
print(q)
The fact it is treated in a similar way to false means you can do things like

Code: Select all

b = (b or 0) + 1
I would recommend reading the PIL if you want a more detailed explanation rather than the reference guide.


Those people booked on my Plugin course next week have all this to look forward to :D

Re: nil - desperandum

Posted: 02 May 2014 11:08
by DavidNewton
Thanks for the very rapid response.

I was in fact reading Programming in Lua when I decided that I was not comfortable with the definition of nil and so I read the reference manual which is, on this topic, almost identical to the PiL. My problem is not to do with the use of nil which I understand moderately well. My problem is with the definition and that is my problem - I find it hard to get to grips with something if I don't have, what I consider to be, a proper explanation of the terminology used (retired mathematician, sorry). I will push on and hope that enlightenment comes later.

David

Re: nil - desperandum

Posted: 02 May 2014 11:27
by AdrianBruce
Oh dear David - you have my sympathy - and I only trained as a mathematician!

Tongue-in-cheek warning ON
"value nil whose main property is to be different from any other value" - personally I thought any value would be different from any other value!

Sorry Jane....
Tongue-in-cheek warning OFF

Re: nil - desperandum

Posted: 02 May 2014 11:46
by DavidNewton
AdrianBruce wrote:
Tongue-in-cheek warning ON
"value nil whose main property is to be different from any other value" - personally I thought any value would be different from any other value!

Sorry Jane....
Tongue-in-cheek warning OFF
Adrian that phrase is precisely what set my alarms buzzing and my reaction to it was exactly the same as yours. The replacement of value by value type might help because I can accept that the nil value type is different from any other value type but then again, this also applies to all the other value types so it cannot uniquely pick out the nil type.

I have to stop thinking about this!

David

Re: nil - desperandum

Posted: 02 May 2014 12:03
by Jane
I suspect the reason the nil definition says main is because you can do things like this

Code: Select all


debug.setmetatable(nil, {
    __index = function(t, i)
        return nil
    end
})

local this = {}

if this.field.does.NOT.exist == nil then
    print('no error!')
end

--> no error!
So although I would not recommend it, explains the "main" part and you can do other things will nil.

Taken from a discussion on the Lua mailing list <lua-l@lists.lua.org> and posted by Andrew Starks. If you really want an answer post to that list. I don't have a problem as long as I can understand how it works, I suppose that's because I trained as a programmer and not a mathematician.

Re: nil - desperandum

Posted: 02 May 2014 12:11
by tatewise
I agree the wording is ambiguous, but I think it is trying to say that the type nil is distinct from the other 7 types.
You have actually omitted an important comma before 'whose':
Nil is the type of the value nil, whose main property is to be different from any other value;
where the 'whose' is meant to refer to the type (not the value) since the paragraph is defining types.

So if you apply the function type() to any value, the only value that has the type nil is the value nil.

As it goes on to say, another property is that nil is considered false.
it usually represents the absence of a useful value.
is meant in the sense that programmers can represent any state or condition with any value if they choose to. So absence of a useful value might be represented by zero 0 or "X", but in Lua nil is often used, and that uninitialised variables are nil is very convenient.

BTW: If you assign nil to a variable it does NOT delete it (unless it is a table or table field).

Re: nil - desperandum

Posted: 02 May 2014 13:22
by DavidNewton
Many thanks Jane & Mike.

I have had another search on the Lua community forum and came up with a number of threads which discuss nil at great length. The most useful thing I got was that I was not alone in being uneasy about nil and the second most useful thing is they it has convinced me to forget about it (in fact that may be the most useful thing)
Regarding the deletion of a variable this is what appears to happen in the plugin editor. For example stepping though the following
a = 1
a = nil
b = 2
After a=nil the next step gives the no variables found message and I took that to mean a had been deleted.

David

Re: nil - desperandum

Posted: 02 May 2014 13:41
by Jane
David, because you are not using local variables, a in your example is one of the global variables which is held in a table (_G) so it does get cleaned up, local variables set to nil will not be removed until then go out of scope.