[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: Propsoal: a lua dialect without nil
- From: Kristofer Karlsson <kristofer.karlsson@...>
- Date: Tue, 15 Feb 2011 13:40:56 +0100
I think the source of problems with nil isn't its existence, but its double meaning. The two senses of nil - nothing is stored here, and a value that is unlike other values - are separate in most other languages. For example, in Python, "nothing is stored here" is signified by raising an exception, and "value that is unlike other values" is signified by None. The problems with nil rise from the fact that it has to handle both cases.
So, I don't think getting rid of nil is the answer. I think the answer lies in a stronger way of defining which values are actually defined, beyond "does it equal nil." I'm borrowing from Python here, but "in" and "del" keywords for testing whether a key is actually *defined* in a table (i.e. "3 in t") and deleting keys from a table (i.e. "del t[3]") might be useful for this. This obviously isn't a complete solution to the problem, but it would definitely sort out a lot of the confusion.
--
Regards, Matthew Frazier
Python does both:
x = map.get("foo") # returns None if not found
x = map["foo"] # raises error if not found
Not sure if this is good or not.
I actually think Luas map model is easier to grasp than Pythons.
In Lua, a table is simply a map from key to value, where value can be anything including nil. There is no hard coded concept of exists or not, that's up to interpretation.
In Python, I can do:
t = dict()
None in t # gives False
t["foo"] # error
t.get("foo"] # gives None
t["foo"] = None
t["foo"] # now gives None
None in t # still gives False
This makes it confusing. Python seems to use None both as a real value and as indicator of absense - since "None in t" will never give true.