lua-users home
lua-l archive

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


> > Still, being able to do something like:
> 
> > function Number.tostring(num)
> >     return "Number: " .. num
> > end
> 
> > num = 55
> > print(num:tostring())
> 
> > is very, very cool.
> 
> I don't think so. I like the functional way I can program 
> with Lua. That is the main reason why I using Lua. If I would 
> like very cool oo-Programming then possible I would change to 
> Ruby. But I dislike all this automatic stuff. I prefer to 
> write long meaningfull variable names, I prefer to write 
> math.sin() instead of sin() and so on.

I think you are missing the point.  Nobody is stopping you from using
functional programming in Lua.  But from a programming standpoint, it
sure is easier (and faster) to write something like:

function DoSomething(value)
	if value.tostring() then
		print(value:tostring())
	end
end

value = 5
DoSomething(value)
value = "String"
DoSomething(value)
value = SomeTableConstructorNew()
DoSomething(value)
value = AnotherTableConstructorNew()
DoSomething(value)
value = GetUserDataFromC()
DoSomething(value)

Than to write:

function DoSomething(value)
	if type(value) == "number" then
		print(num_tostring(value))
	elseif type(value) == "string" then
		print(string_tostring(value))
	elseif metatable(value) == SomeTableType then
		print(SomeTableType_tostring(value))
	-- and so on...
end

In this example, you have to cover the case of EVERY type of value
you'll ever possibly pass in to DoSomething().  With metatable support
(and hence, some built-in OO-like abilities), I can write a simplistic
DoString(), as in the above example.

I prefer the first approach, by a long shot.

> > For Sol, those metatables exist as regular tables called Number, 
> > String, etc.  This is an extension of the Lua 4.1 work4 mechanism
> 
> > (which I hope the Lua team will adopt)
> 
> I hope they will not going in this direction. Instead they 
> should concentrate on a extreme stable, free of bugs, well 
> defined base, good documentation, ...

For what it's worth, Sol's implementation of this is just a few lines of
code.  LuaState's implementation is an almost direct mirror of Sol's.
It isn't some big architectural overhaul of Lua.

Josh