[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: Colon notation in object-oriented programming
- From: Petri Häkkinen <petrih3@...>
- Date: Sun, 5 Mar 2017 15:25:05 +0200
On Mar 5, 2017, at 2:58 PM, 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)
And to extend on my reply a little bit… I don't think the difference between these two versions matter at all. Most of the CPU time is spent in file i/o and table.concat anyway (in the other thread I gave some examples where the speed difference can matter).
The original question was about object composition and extending the ':' notation and I was trying to say that it's actually valid to separate code and data (I.e. not use OOP) and just use free functions to do your thing. I.e. instead of doing object.system:do_a_thing() or whatever just call object_system_do_a_thing() and pass whatever data you need in. You can still think of your data as objects, it's just the code that is written in imperative style. Simple.
The other ramblings I had were just about giving some ideas to people who may be too accustomed to think in only classes & object (I used to be one).
Hopefully I'm not sounding arrogant or besserwisser or anything like that. Just trying to be helpful.
Petri