nil - desperandum
Posted: 02 May 2014 10:31
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