[Date Prev][Date Next][Thread Prev][Thread Next]
- Subject: Re: upvalues and polymorphism
- From: lhf (Luiz Henrique de Figueiredo)
- Date: Fri, 15 May 1998 08:21:44 -0300
>From email@example.com Fri May 15 01:10:23 1998
>I hate to stray more toward the Python crowd, but
>I couldn't help noticing that upvalues seem like
>a way to introduce object-oriented like programming
>capability into lua.
no, upvalues are ways to create anonymous functions with different internal
constants, or in other words, functions with their own enclosing environment.
if you have to label it, upvalues and anonymous functions go more towards
functional programming than towards object-oriented programming.
tag methods on the other hand, do help in implementing object-oriented stuff.
>So the question is, why not
>then make it so one is able to define functions that
>are called differently depending on the tag of the
this is possible already.
just model objects with tables. then o:f(x,y,z)is sugar for o.f(o,x,y,z)
and you can do whatever you want to create a function value for o.f.
this uses tags methods, of course.
>For example, I could define
>myclass = newclass() -- really would be a "newtag"
>myclass = newclass(oldclass) -- same as newtag() except all
> -- methods would be copied from the old class.
if old then copytagmethods(old,new) end
copytagmethods is new in 3.1. in previous versions, you can explicitly copy all
methods needed with settagmethod(new,gettagmethod(old,x),x)
>e = myclass(5,4,6) -- would call constructor method for myclass
> -- with parameters 5,4,6
ok, then you want newclass above to create and returna a table, set its tag
and also set the "function" tag method so that you trap this.
>then I could define a function like
>To be invoked as e.mymethod(5,6,7)
can't you do this already?
>You could even have constructors/destructors like
>or function ~myclass
> (called during garbage collection)
if you must use C++ syntax, then ok, all this can be done with tag methods,
except for using ~myclass.
>Perhaps I am missing the point on how upvalues should
like I said, upvalues are not really the point here, althoug they could be
>but it seems to me that invoking methods by
>data type is a very common occurrence in most languages,
sure, and that's why we have tag methods in Lua.
granted, using tag methods can get complicated sometimes, depending on what
you really want to do. the point is that many things that seem to depend on
a language change are already possible with tag methods.
>I don't want to turn lua in to a scripted C++ but lua
>language. I think lua's strength is its simplicity so
>if these types of features could be added without language
>bloat that would be a great thing.
I agree 100%. To quote our home page:
A fundamental concept in the design of Lua is to provide
<EM>meta-mechanisms</EM> for implementing features, instead of
providing a host of features directly in the language. For example,
although Lua is not a pure object-oriented language, it does provide
meta-mechanisms for implementing classes and inheritance.
Lua's meta-mechanisms bring an economy of concepts and keep the
language small, while allowing the semantics to be extended in
unconventional ways. Extensible semantics is a distinguishing feature
The implementation goals are simplicity, efficiency, portability, and
low embedding cost.