lua-users home
lua-l archive

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


> I don't think {} or metatable index
> would work here (also, while I can't think of a use for real metatables 
on
> an xml tag right now, I'm sure there'd be some application where we'd 
regret
> that choice if we hijacked that entry).

I'm not proposing putting a metatable on an xml tag. I'm proposing putting 
it
on an XML node. Referencing the tag-object from the node-object's 
metatable
(or making the node-object's metatable the tag-object) seems to me to 
perfectly
capture what the relationship between a tagtype and a node-object is.

Here is a simple example, typed in a hurry. I have a more complete 
implementation.


xmltags = {}

xmltag_meta = {}
function xmltag_meta:__call(att)
  return function(...)
    arg.attribute = setmetatable(att, {__index = self.default_atts})
    return setmetatable(arg, self)
  end
end

function newtag(name)
  local tag = setmetatable({tagname = name, default_atts = {}}, 
xmltag_meta)
  xmltags[name] = tag
  return tag
end

xml = memoise(newtag)

function set_default_att(tagname, att, value)
  xml[tagname].default_atts[att] = value
end