Advantages of using classes over tables for OOP

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
User avatar
guy
Prole
Posts: 11
Joined: Wed Sep 19, 2018 7:10 pm

Advantages of using classes over tables for OOP

Post by guy »

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?
User avatar
raidho36
Party member
Posts: 2063
Joined: Mon Jun 17, 2013 12:00 pm

Re: Advantages of using classes over tables for OOP

Post by raidho36 »

Classes are tables.
User avatar
ivan
Party member
Posts: 1915
Joined: Fri Mar 07, 2008 1:39 pm
Contact:

Re: Advantages of using classes over tables for OOP

Post by ivan »

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.
TheHUG
Citizen
Posts: 62
Joined: Sun Apr 01, 2018 4:21 pm

Re: Advantages of using classes over tables for OOP

Post by TheHUG »

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.
User avatar
ivan
Party member
Posts: 1915
Joined: Fri Mar 07, 2008 1:39 pm
Contact:

Re: Advantages of using classes over tables for OOP

Post by ivan »

There are no classes in Lua - just tables and userdata objects.
User avatar
guy
Prole
Posts: 11
Joined: Wed Sep 19, 2018 7:10 pm

Re: Advantages of using classes over tables for OOP

Post by guy »

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!
Post Reply

Who is online

Users browsing this forum: Amazon [Bot], Bing [Bot] and 11 guests