equivilant of 'index' tag method for globals?

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

equivilant of 'index' tag method for globals?

David Jeske-2
I feel like I should be able to do this, but I can't see how to do it
with the existing fallbacks. I want to have a tag method called when a
global variable dosn't exist.

I want to do this because I want to separate 'enum' types from the
current global table, but I want them to have global scope. In the
end, it will look something like this:

enums = { 
    dog_names = {
          ROVER = 1,
          SPOT  = 2,
          FIDO = 3
    },
    car_makes = {
          HONDA = 1,
          FORD = 2,
          NISSAN = 3
    }
}

function e_type(varname) 
  local enum_value
  local enum_name

  enum_value,enum_name = find_enum(varname);
  return enum_name
end

print(ROVER) -- 1
print(NISSAN) -- 3
print(e_type("ROVER")) -- dog_names

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

Reply | Threaded
Open this post in threaded view
|

Re: equivilant of 'index' tag method for globals?

Roberto Ierusalimschy
  settagmethod(tag(nil), "getglobal", <your function>)


<your function> will be called with the name of the global variable and
its original value (nil in this case). (I suppose it is "find_enum" in your
case).

-- Roberto

Reply | Threaded
Open this post in threaded view
|

Re: equivilant of 'index' tag method for globals?

Steve Dekorte-5
In reply to this post by David Jeske-2
On Tue, 1 Dec 1998, David Jeske wrote:

> I feel like I should be able to do this, but I can't see how to do it
> with the existing fallbacks. I want to have a tag method called when a
> global variable dosn't exist.
> 
> I want to do this because I want to separate 'enum' types from the
> current global table, but I want them to have global scope. In the
> end, it will look something like this:

I think this will work:

settagmethod(tag(nil), "getglobal",
 function (varname)
    local value = gettable(rawgetglobal("enums"), varname)
    if ( value ~= nil ) then return value end
    return rawgetglobal(varname)
 end )

I use this sort of thing to move Lua's primatives out of globals and 
into a organized namespace.

---
Steve Dekorte 

Reply | Threaded
Open this post in threaded view
|

Re: equivilant of 'index' tag method for globals?

Steve Dekorte-5
In reply to this post by David Jeske-2
Oops, I forgot that I don't need to try to look up the global, since this
tag method only gets called when that global is nil. So this is all 
that's needed:

settagmethod(tag(nil), "getglobal",
  function (varname) 
    return rawgettable( rawgetglobal(enms), varname)
  end )

---
Steve Dekorte 

Reply | Threaded
Open this post in threaded view
|

Re: equivilant of 'index' tag method for globals?

David Jeske-2
In reply to this post by Roberto Ierusalimschy
On Tue, Dec 01, 1998 at 04:21:11PM -0200, Roberto Ierusalimschy wrote:
>   settagmethod(tag(nil), "getglobal", <your function>)
> 
> <your function> will be called with the name of the global variable and
> its original value (nil in this case). (I suppose it is "find_enum" in your
> case).

Thanks, that worked great. I wasn't understanding that it was actually
calling the 'getglobal' tag method on the "nil" which is returned
because the variable isn't there. I'm all straightened out now.

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