Advantages of using classes over tables for OOP
Forum rules
Before you make a thread asking for help, read this.
Before you make a thread asking for help, read this.
Advantages of using classes over tables for OOP
I've been using love for a while now, and I've mostly just used tables for everything object-oriented. I started looking at source codes for other love games and I see many people using classes for oop. What are the advantages of using classes over tables? Does it really matter?
Re: Advantages of using classes over tables for OOP
Classes are tables.
Re: Advantages of using classes over tables for OOP
Your terminology is slightly off. I think you mean the advantages of metatables vs no metatables. There are no "classes" in Lua although we do have tables and userdata objects that "look" like classes.
Re: Advantages of using classes over tables for OOP
One pretends to be a class, the other admits its just a table.
You can get all the advantages of a class, with all the added flexibility of lua tables using the __index metamathod. I think the main advantage of using a library that mimics classes for you is familiarity with classes from other languages. The other is probably that you don't have to re-implement the initialization method for each project you start (though it's just 3-ish lines each time). Python, like lua, has classes that are basically a hash table of attributes, unlike lua however, python tries very hard to make them look and behave like regular classes. I think that is a mistake.
I find tables to be more flexible than classes. You can dynamically change the methods of an object, and apply any method of one table to any other table, you always have a way to get out of the kind of corners you can code yourself into with OOP. If you try to treat your tables too much like classes, using these advantages always feels kind of awkward to me.
That's just my opinion on it, you should try both ways and decide for yourself. I have to admit I didn't give lua class implementations that much of a chance.
You can get all the advantages of a class, with all the added flexibility of lua tables using the __index metamathod. I think the main advantage of using a library that mimics classes for you is familiarity with classes from other languages. The other is probably that you don't have to re-implement the initialization method for each project you start (though it's just 3-ish lines each time). Python, like lua, has classes that are basically a hash table of attributes, unlike lua however, python tries very hard to make them look and behave like regular classes. I think that is a mistake.
I find tables to be more flexible than classes. You can dynamically change the methods of an object, and apply any method of one table to any other table, you always have a way to get out of the kind of corners you can code yourself into with OOP. If you try to treat your tables too much like classes, using these advantages always feels kind of awkward to me.
That's just my opinion on it, you should try both ways and decide for yourself. I have to admit I didn't give lua class implementations that much of a chance.
Re: Advantages of using classes over tables for OOP
There are no classes in Lua - just tables and userdata objects.
Re: Advantages of using classes over tables for OOP
Yeah I should have been more clear. I knew that there aren't classes in lua, I meant libraries that implement them. But I understand the concept a lot more now, thanks for the help!
Who is online
Users browsing this forum: Amazon [Bot], Bing [Bot], Semrush [Bot] and 3 guests