lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


Miles Bader <miles@gnu.org> writes:
> E.g., callable(...), indexable(...) etc.
>
> Maybe a single general predicate would work, e.g., something like
> "applicable (OBJ, KEY)" where KEY is the assocated metamethod ('__call',
> '__index', etc).

Here's an example:

   local applicable_prims = {
      __call = 'function', __index = 'table', __newindex = 'table',
      __add = 'number',
      -- ...etc...
   }

   function applicable (obj, key)
      if applicable_prims[key] == type (obj) then
         return true
      else
         local mt = getmetatable (obj)
         return mt and mt[key]
      end
   end

-miles

-- 
"Don't just question authority,
Don't forget to question me."
-- Jello Biafra