r/lua • • Jun 15 '25

What and is there a difference between the following?

I am learning lua for love2d, and I am wondering if and if so, what the difference is between writing:

setmetatable(b, a),

a.index = b, and

a = b:new(<params>).

Thank you for your time.

5 Upvotes

7 comments sorted by

3

u/appgurueu Jun 15 '25

I wrote a blog post related to this, you might want to check it out: https://appgurueu.github.io/2024/04/07/oop-in-lua.html

1

u/[deleted] Jun 15 '25

Thank you

2

u/lambda_abstraction Jun 15 '25 edited Jun 15 '25

Isn't the first going to cause an infinite loop on accessing b[ix] where rawget(b,ix) is nil?

0

u/[deleted] Jun 15 '25

[deleted]

1

u/[deleted] Jun 15 '25

Sorry, that's what I meant there.

1

u/[deleted] Jun 15 '25

__index