lua-users home
lua-l archive

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

>Well, it is expected behavior. Upvalues apply only to variables, not to
>fields. When you write %t.a, the binding is (%t).a, and not %(t.a); so,
>only the t is frozen, not the field value. If you really want to run

Yes, but since t is frozen, this means that the value of t is frozen,
so I expected fields value to be part of table value, but then this is
arguable, and I do not know of how this is implemented.
So you can see why I was doing this, I was trying to  map C++ OO syntax
to Lua syntax to show how Lua can be used for OO programming even if it
was not meant to at first (sorry, comments in french). That is why I
needed both upvalues in table fields and t:f style declaring.


-- initialise le robot de base, qui sait avancer et tourner
function init_basic_robot(robot)
  robot.type="Robot de base"

  -- definit la position du robot

  -- les methodes
  robot.forward = function(self,distance)
    self.xpos = self.xpos + sin(distance)
    self.ypos = self.ypos + cos(distance)
    print("Basic robot advancing "..distance)
  robot.rotate = function(self,angle)
    self.angle = self.angle + angle

-- initialise le robot avance, qui sait faire tout ce que fait le robot
-- base, et sait egalement compter la distance totale parcourue
function init_advanced_robot(robot)
  local parent_member

  -- le robot avance est aussi un robot de base

  -- la distance totale n'est pas disponible pour le robot de base
  robot.total_distance = 0.0

  -- surcharge la methode forward et appelle l'object parent par
  -- l'intermediaire d'une upvalue
  parent_member = robot.forward
  robot.forward = function(self,distance)
    print("Advanced robot advancing "..distance)
    self.total_distance = self.total_distance + distance

-- create both robots

-- make them walk forward


>that code, you need a temporary variable:
>  t.a = function(s)
>    print(s)
>  end
>  local ta = t.a
>  t.a = function(s)
>    print(s)
>    %ta(s)
>  end

Yes, that's the way I did it so I can get around the syntax problem,
but I was just wondering if there was another way of doing it, so it
would look even more C++ like. Hope you're not angry about that ;)
I already had private members implemented via tag methods, and I'd
liked to get inheritance without temporaries :)

Thanks for your help !