* nil - desperandum
- DavidNewton
- Superstar
- Posts: 462
- Joined: 25 Mar 2014 11:46
- Family Historian: V7
nil - desperandum
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
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
- Jane
- Site Admin
- Posts: 8442
- Joined: 01 Nov 2002 15:00
- Family Historian: V7
- Location: Somerset, England
- Contact:
Re: nil - desperandum
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
The fact it is treated in a similar way to false means you can do things like
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
So for example you might do
Code: Select all
local q
for i = 1,50 do
print (i)
q = i
end
print(q)
Code: Select all
b = (b or 0) + 1Those people booked on my Plugin course next week have all this to look forward to
Jane
My Family History : My Photography "Knowledge is knowing that a tomato is a fruit. Wisdom is not putting it in a fruit salad."
My Family History : My Photography "Knowledge is knowing that a tomato is a fruit. Wisdom is not putting it in a fruit salad."
- DavidNewton
- Superstar
- Posts: 462
- Joined: 25 Mar 2014 11:46
- Family Historian: V7
Re: nil - desperandum
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
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
- AdrianBruce
- Megastar
- Posts: 1962
- Joined: 09 Aug 2003 21:02
- Family Historian: V7
- Location: South Cheshire
- Contact:
Re: nil - desperandum
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
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
- DavidNewton
- Superstar
- Posts: 462
- Joined: 25 Mar 2014 11:46
- Family Historian: V7
Re: nil - desperandum
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.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
I have to stop thinking about this!
David
- Jane
- Site Admin
- Posts: 8442
- Joined: 01 Nov 2002 15:00
- Family Historian: V7
- Location: Somerset, England
- Contact:
Re: nil - desperandum
I suspect the reason the nil definition says main is because you can do things like this
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.
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!
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.
Jane
My Family History : My Photography "Knowledge is knowing that a tomato is a fruit. Wisdom is not putting it in a fruit salad."
My Family History : My Photography "Knowledge is knowing that a tomato is a fruit. Wisdom is not putting it in a fruit salad."
- tatewise
- Megastar
- Posts: 27087
- Joined: 25 May 2010 11:00
- Family Historian: V7
- Location: Torbay, Devon, UK
- Contact:
Re: nil - desperandum
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':
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.
BTW: If you assign nil to a variable it does NOT delete it (unless it is a table or table field).
You have actually omitted an important comma before 'whose':
where the 'whose' is meant to refer to the type (not the value) since the paragraph is defining types.Nil is the type of the value nil, whose main property is to be different from any other value;
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.
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.it usually represents the absence of a useful value.
BTW: If you assign nil to a variable it does NOT delete it (unless it is a table or table field).
Mike Tate ~ researching the Tate and Scott family history ~ tatewise ancestry
- DavidNewton
- Superstar
- Posts: 462
- Joined: 25 Mar 2014 11:46
- Family Historian: V7
Re: nil - desperandum
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
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
- Jane
- Site Admin
- Posts: 8442
- Joined: 01 Nov 2002 15:00
- Family Historian: V7
- Location: Somerset, England
- Contact:
Re: nil - desperandum
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.
Jane
My Family History : My Photography "Knowledge is knowing that a tomato is a fruit. Wisdom is not putting it in a fruit salad."
My Family History : My Photography "Knowledge is knowing that a tomato is a fruit. Wisdom is not putting it in a fruit salad."