[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: RE: nil isn't as unique in Lua 4.1
- From: RLake@...
- Date: Thu, 10 Jan 2002 15:41:04 -0500
Roberto Ierusalimschy escribió:
> On Thu, 10 Jan 2002, Eric Ries wrote:
> > I'm afraid that, if Lua goes down this path, we will have the need for
> > Perl6's "defined-shortcut-operator" which works "like || but with a
twist"
> I think that, more often than not, you know what type of object you
> are expecting. So, if you are expecting numbers, or tables, or strings,
> you still can use the "or" to select the first "defined" value.
That's true, but it's also an obscure bug waiting to bite you. It's
actually quite
difficult to combine the need for simplicity with the desire for boolean
operators.
Personally, I would have prefered a different solution: leave "and" and
"or" as they are, and define "false" as a distinct value. That means that
false would be true, which I suppose would be counter-intuitive, but you
could get around that with a test for false (x ~= false, actually), which
could be a prefix operator. Actually, the only case in which you really
need false to be a non-nil but false value is when you want to stuff it
into a table, and you need to be able to distinguish between false, true
and unspecified: in other words, false is needed for three-valued logic.
That's fine, but we find ourselves without standard operators for
three-value logic, useful though they might be.
> In the few cases that a false is among the possible outcomes, then we
> will have to go through the pain of writing "if a == nil then ... ".
So what about the default values question? In the case where I want a to
have a default,
I think that the following works, as an intellectual curiosity. I played
around with the three-valued truth tables, and I think that this captures
all the possibilities where a or default might be false.
... (a or (a == nil and default)) ...
This would not turn into my favourite idiom, I'm afraid.
What if I want to set a to the first of (a, f1(), f2(), ...) which has a
value without unnecessarily evaluating f() or g()? The above works for the
case where there is one f(), since default is only evaluated once, but I
can't pull that trick more than once because a needs to be evaluated twice.
Then I think in that case there is no expression solution, only the nested
if:
if a == nil then
a = f()
if a == nil then
a = g()
end
end
That's a lot wordier than
a or f() or g()
(i.e. it requires some careful reading to know what's meant)
> > Notice that Lua already has a similar situation: the code
> (a and b) or c
> is equivalent to "a ? b : c", provided that b is not nil (or false, now).
> Again, frequently we know that b cannot be nil, and so the idiom works
fine.
But presumably if you're using boolean values, one of the two cases you are
interested in is the one where b in fact is "false". I don't think there is
a good general solution with binary operators, though. Ternary operators
like C's ?: operator do handle the situation, but I'm not a huge fan of the
syntax.
Question: why are there no languages which define the following construct:
value2 when condition other value2
which associates to the right quite readably:
a = 0 when x < 0 otherwise
1 when x < 1 otherwise
foo(x)
Or do I just have a wierd mind?
R.