lua-users home
lua-l archive

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


It was thus said that the Great Luiz Henrique de Figueiredo once stated:
> > it seems that I need to add an entry to the "Value" union in lobject.h,
> 
> Without memory allocation, that approach cannot work because the
> fields correspond to *values*, not objects, and so "foo.x = 3" can
> never affect the value of "foo". Note that this is true even when
> "foo" contains a table:  "foo.x = 3" changes to contents of the table
> but not the contents of "foo".

  That doesn't follow.  I can do:

	mt = { __index = function(_,k) if k == 'print' then return print end end }
	debug.setmetatable(3,mt)
	x = 42
	x:print()

so it seems that if John W creates a new type VECTOR (like LUA_TNUMBER) then
attaching a metatable for the VECTOR type would work.  Something like:
in Lua, but I would expect this to be in C:


	vector_meta = 
	{
	  __index = function(self,k)
	    if k == 'x' then
	      -- magic code to extract the x portion in the value self
	    elseif k == 'y' then
	      -- magic code to extract the y portion in the value self
	    end
	  end,

	  __newindex = function(self,k,v)
	    if k == 'x' then
	      -- magic  code to set x portion of self to v
	    elseif k == 'y' then
	      -- magic code to set y portion of self to v
	    end
	  end
	}
	
  What am I missing?
  
  -spc