lua-users home
lua-l archive

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


>From lua-l@tecgraf.puc-rio.br  Thu Mar  9 10:57:41 2000
>From: "Bru, Pierre" <Pierre.Bru@spotimage.fr>
>
>I want to have an "object" that intercept unknown "methods" call, calling a
>known error handler. 
>
>for ex: TObject:unkMethod() calls TObject:DoesNotUnderstand
>
>I tried this (see below) but I get an error on the last line.
>how can I do that ?
>
>------------------------------------------
>
>Object=newtag()
>TObject = {}
>settag( TObject, Object )
>function TObject:DoesNotUnderstand ( ... )
>  print('DoesNotUnderstand')
>end
>settagmethod( Object, "function", TObject:DoesNotUnderstand )

The error in the last line is that the syntax t:m is only for defining or
calling methods. To refer to a method, you use t.m.
So, settagmethod( Object, "function", TObject.DoesNotUnderstand ) does
work, in the sense that it compiles without error.

Still, it does not do what you want, because t:m() is sugar for t.m(t.m)
and so if m does not exist in table t, then the tag methods that are called
are for gettable and then for index. The "function" is only called after these
return something that is not a function (and which has the correct tag).
So, you need something like

function TObject:DoesNotUnderstand1 ( ... )
  print('DoesNotUnderstand1')
  return TObject
end

settagmethod( Object, "function", TObject.DoesNotUnderstand )
settagmethod( Object, "index", TObject.DoesNotUnderstand1 )

--lhf