lua-users home
lua-l archive

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



Sean Conner <sean@conman.org>于2017年3月6日 周一上午6:25写道:
It was thus said that the Great Dirk Laurie once stated:
> 2017-03-04 8:32 GMT+02:00 Petri Häkkinen <petrih3@gmail.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:
>
> io.open("results.txt","w"):write(table.concat(results,"\n")):close()

  I tried that code and I got:

lua: /tmp/xx.lua:3: attempt to index a nil value
stack traceback:
        /tmp/xx.lua:3: in main chunk
        [C]: ?

It took me awhile [1] to figure out the problem, and I had to rewrite the
program:

It should be:

assert(io.open("results.txt","w")):write(table.concat(results,"\n")):close()

when you use in production code.

--
regards,
Xavier Wang.