lua-users home
lua-l archive

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


>From dmarks@dionysus.phs.uiuc.edu 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
>first parameter.  

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.

function newclass(old)
	new=newtag()
	if old then copytagmethods(old,new) end
	return 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
>
>function myclass:mymethod(a,b,c)
>local x,y,z
> ...
>end
>
>To be invoked as e.mymethod(5,6,7)

can't you do this already?

>You could even have constructors/destructors like
>
>function myclass
>local x,y,z
>
>end
>
>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
>be used,

like I said, upvalues are not really the point here, althoug they could be
useful too.

>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

good! :-)

>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
 of Lua.

 The implementation goals are simplicity, efficiency, portability, and
 low embedding cost.

--lhf