lua-users home
lua-l archive

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


>>> leiradella@bigfoot.com 01/20/05 05:46AM >>>
>
> screen = Screen(640, 480)
>
> But it allows for only one constructor. 

We don't have compile-time overloading but Lua
functions can check the type of their parameters
and work out what kind of construction
to perform:

function Point.set(pt,x,y,z)
  -- copy constructor?
  if type(x) == 'table' and getmetatable(x) == Point then
     local po = x
     x = po[1]
     y = po[2]
     z = po[3]
  end
  pt[1] = x
  pt[2] = y
  pt[3] = z 
end

Granted, this could get ugly.

On another tangent, I've defined Point and Complex
classes.  Shows how to redefine the Lua arithmetic 
operators consistently:

http://lua-users.org/wiki/PointAndComplex 

steve d.