non-empty false...

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

non-empty false...

David Jeske-2
I'm trying to put together a non-empty false value, and I can't figure
out a way to do it. It seems to me that in order to do this I would
need a tag method which is used for boolean comparison (as opposed to
assignment). That way I could do:

FALSE = {}
local a_tag = newtag();
settag(FALSE,a_tag)

settagmethod(a_tag,"bool",
    function (a_table) 
       return nil;               
    end
   );

Do any of you know an existing method for getting this kind of behavior?

=====

I have three reasons for wanting to do this:

1) My first motivation for this is that in proto-style "parent"
inheritence schemes, any values which are "nil" are not-present, which
means that accessing them requires a search of the entire inheritence
tree, just to not find the nil value. 

2) My second motivation is that Lua "nil" false values confuse the nice
proto-override style inheritence behavior. That is, if I have:

  super_obj = {
     foo_integer = 1
     bar_boolean = 1
  }

  child_obj = {
     parent = super_obj
  }

Then the following works great:

 print(child_obj.foo_integer) -- 1
 child_obj.foo_integer = 2
 print(child_obj.foo_integer) -- 2
 child_obj.foo_integer = nil
 print(child_obj.foo_integer) -- 1

However, booleans are confused:

 print(child_obj.bar_boolean) -- 1
 child_obj.bar_boolean = nil       -- i.e. false
 print(child_obj.bar_boolean) -- 1

In other words, there is no way to set a "false" value for the
variable.

3) And finally, my third motivation. I'm trying to come up with a
scheme to 'cache' a parent inheritence heirarchy. I want to do this
because multi-level and multi-parent heirarchies take a long time to
search, so instead I'd like to flatten a given heirarchy, and only
reparse it when it changes. However, this only works well if the
heirarchy is changed infrequently, and any boolean value stored in the
heirarchy makes items appear and disappear frequently, causing
thrashing of the cache.

-- 
David Jeske (N9LCA) + http://www.chat.net/~jeske/ + [hidden email]

Reply | Threaded
Open this post in threaded view
|

Re: non-empty false...

Steve Dekorte-5
On Tue, 1 Dec 1998, David Jeske wrote:

> I'm trying to put together a non-empty false value, and I can't figure
> out a way to do it. It seems to me that in order to do this I would
> need a tag method which is used for boolean comparison (as opposed to
> assignment). That way I could do:
> 
> Do any of you know an existing method for getting this kind of behavior?

You could have global true and false objects and use an "isEqual_" method
in place of "==".

true = {
    isEqual_ = function (self, o) 
	if ( self == o ) then return 1 end
	return 0
}

false = clone(true)

> I have three reasons for wanting to do this:
> 
> 1) My first motivation for this is that in proto-style "parent"
> inheritence schemes, any values which are "nil" are not-present, which
> means that accessing them requires a search of the entire inheritence
> tree, just to not find the nil value. 

how about using "Nil" instead of nil and making Nil = 0?
(Just use 0 instead off nil for false boolean values)
 
Steve 

Reply | Threaded
Open this post in threaded view
|

Re: non-empty false...

David Jeske-2
On Tue, Dec 01, 1998 at 07:31:51PM -0200, Steve Dekorte wrote:
> > I'm trying to put together a non-empty false value, and I can't figure
> > out a way to do it. It seems to me that in order to do this I would
> > need a tag method which is used for boolean comparison (as opposed to
> > assignment). That way I could do:
> > 
> > Do any of you know an existing method for getting this kind of behavior?

> You could have global true and false objects and use an "isEqual_" method
> in place of "==".

> how about using "Nil" instead of nil and making Nil = 0?
> (Just use 0 instead off nil for false boolean values)

Neither of these solutions work well with standard binary
operations. Plus, I don't want to have to bloat the syntax with object
calls everywhere for simple operation. What you're suggesting would
turn this:

  if (a_value or another_value) then

into this:

  if (a_value:or(another_value):isEqual(TRUE)) then

No thanks... I'd rather put a proper boolean type into Lua than
confuse up the syntax. I was just hoping for a simple way to do it
within the Lua runtime meta-mechanisms.

-- 
David Jeske (N9LCA) + http://www.chat.net/~jeske/ + [hidden email]

Reply | Threaded
Open this post in threaded view
|

Re: non-empty false...

Steve Dekorte-5
On Tue, 1 Dec 1998, David Jeske wrote:

> No thanks... I'd rather put a proper boolean type into Lua than
> confuse up the syntax. I was just hoping for a simple way to do it
> within the Lua runtime meta-mechanisms.

I also suggested using 0 instead of nil.
What about that? 

---
Steve Dekorte 

Reply | Threaded
Open this post in threaded view
|

Re: non-empty false...

David Jeske-2
On Tue, Dec 01, 1998 at 08:54:15PM -0200, Steve Dekorte wrote:
> On Tue, 1 Dec 1998, David Jeske wrote:
> > No thanks... I'd rather put a proper boolean type into Lua than
> > confuse up the syntax. I was just hoping for a simple way to do it
> > within the Lua runtime meta-mechanisms.
> 
> I also suggested using 0 instead of nil.
> What about that? 

That's what I'm doing now, but it means I have to do things like:

function isTrue(a_var) 
    -- make sure that both nil and 0 are false... too bad
    -- lua dosn't have exceptions, or I'd throw one on nil.
  if ((a_var == nil) or (a_var == 0)) then
     return nil;
  else
     return 1;
  end
end

if (isTrue(foo)) then
  -- do something
end

-- worse, yet, I have to do stuff like:

if (isTrue(foo) and isTrue(bar)) or isTrue(foo2) then
  -- do something
end

-- 
David Jeske (N9LCA) + http://www.chat.net/~jeske/ + [hidden email]