This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
Sewbacca's profile picture

Some questions about classes

Started by Sewbacca, 07 November 2016 - 09:24 PM
Sewbacca #1
Posted 07 November 2016 - 10:24 PM
I knew classes a long time ago and i played with them, but i ever saw a big security lag on it. Now, after recoding my thread API with an half of the old size, i saw the power in classes, so:

Hey guys,
I have some questions about classes:

What information should i save in classes?
What is with other objects in classes?
Which metamethods are the important ones for classes except __index?
When are classes more usefull than APIs/utilites?
When are classes less usefull then APIs/utilites?
How could i implement static methods?
Why CC uses just enclosures (i dunno the exact name for it)?
Are there common usefull techniques in reference with classes?

Thanks in advance
Sewbacca
Lupus590 #2
Posted 08 November 2016 - 09:18 AM
if you number those questions people can answer a few of them in a clear manner.

As for "How could i implement static methods?" metatables, put the method in the meta table and get the class to check it's metatable if it doesn't know the function that is being looked up.

"Which metamethods are the important ones for classes except __index?" __newindex you may find this useful, make sure to check out the 'See Also' section: http://lua-users.org/wiki/MetamethodsTutorial
Edited on 08 November 2016 - 08:23 AM
Bomb Bloke #3
Posted 08 November 2016 - 10:59 AM
Why CC uses just enclosures (i dunno the exact name for it)?

Are you talking about closures, the sort of functions relied upon within eg the window API? CC doesn't "just" use these - see eg the vector API for examples of objects without closures.

Creating terminal objects via metatables doesn't work with term.redirect(), for whatever reason. I don't know why, but I suspect it may be related to the way pairs() won't pick up on table contents "added" via a metatable.

What information should i save in classes?

Anything that relates directly to the objects they define.

When are classes more usefull than APIs/utilites?
When are classes less usefull then APIs/utilites?

These strike me as the wrong sort of question? I can't think of a case where you'd want to use a class "instead of" a regular API. I can think of plenty of cases where you might want to use "classes" via APIs, though, such as the two links I provided above.

Personally, my attitude is "avoid OOP in Lua". You'll take a speed hit no matter how you try and implement it. The benefits are that it becomes somewhat easier to lump code off into multiple files, but as a general rule I'm against that too (at least for smaller projects - a few thousand lines or less for eg). If you reckon you'll be using the same code for multiple projects then fine, put that code into an API which either project can use, but otherwise the main differences are in code readability… and reading tons of short source files isn't convenient.

If you're asking whether it's a good idea to store unique information directly within an API (as opposed to within an object that API might return), then you should specifically avoid doing that. Generally such code will break if people try to run it multiple times via eg separate multishell tabs.