lua-users home
lua-l archive

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




On 05/03/17 11:48 AM, Dirk Laurie wrote:
2017-03-05 16:42 GMT+02:00 Peter Aronoff <telemachus@arpinum.org>:
Petri Häkkinen <petrih3@gmail.com> wrote:
On Mar 5, 2017, at 1:32 PM, Dirk Laurie <dirk.laurie@gmail.com> wrote:
io.open("results.txt","w"):write(table.concat(results,"\n")):close()

Without colon notation, how far can we get?
If there we no implicit self and no ':' sugar this would be written
something like this:

local file = io.open("results.txt", "w")
io.write(file, table.concat(results, "\n"))
io.close(file)
For what it’s worth, as someone who came to Lua without much OO experience,
I do exactly this now. I find procedural (or imperative) style far clearer.
Have either of you adherents of `io.write(file, table.concat(results, "\n"))`,
one of whom claims to "do exactly this now", actually tried running that
line of code? If not, you have missed the point of that example and in
fact of my post.


Have you ever tried:

local myfile = {}

function myfile:write(...)
  local x = #self
  local t = {...}
  for i=1, select('#', ...) do
    self[x+i] = t[i]
  end
end

function myfile:close()
end

your_awful_function(myfile)

--
Disclaimer: these emails may be made public at any given time, with or without reason. If you don't agree with this, DO NOT REPLY.