[Date Prev][Date Next][Thread Prev][Thread Next]
- Subject: Re: Colon notation in object-oriented programming
- From: Sean Conner <sean@...>
- Date: Sun, 5 Mar 2017 17:25:13 -0500
It was thus said that the Great Dirk Laurie once stated:
> 2017-03-04 8:32 GMT+02:00 Petri Häkkinen <email@example.com>:
> > Why all the trouble? I mean, you could just use free functions:
> > object_system_func(a, b, c)
> > This is faster (no need for nested dynamic table look ups) and
> > there is no need for "self" parameter that is magically more
> > important than the others.
> I and others replied to the above in the thread "Lua and composition",
> but maybe it deserves a thread of its own.
> Let me take an example from the only userdata objects supplied with
> Lua, namely files.
> One can write:
I tried that code and I got:
lua: /tmp/xx.lua:3: attempt to index a nil value
/tmp/xx.lua:3: in main chunk
It took me awhile  to figure out the problem, and I had to rewrite the
file,err = io.open("results.txt","w")
if not file then
-- because io.write() doesn't work as expected
_,err = file:write(table.comcat(results,"\n"))
if not _ then
And then when I ran the code:
results.txt: Permission denied
Object chaining like that is fine when there are no errors. When there are,
it can be difficult to isolate the problem. This point is orthogonal to
your point though.
> Without colon notation, how far can we get?
> local outfile = io.open("results.txt","w")
> Now what next? Remember, outfile:write() is not available.
> And io.write does not allow you to specify a file.
And *that* is the biggest surprise for me in all this. That io.write()
isn't the same as file:write() (just shows you how often I use io.write()).
-spc (Some people love OOP, some people don't)
 Not really, because this is a contrived example