1

To be to the point; I've done Lua for awhile, but never quite got the terminology down to specifics, so I've been Googling for hours and haven't come up with a definitive answer.

Related to OOP in Lua, the terminology used include:

  • Object
  • Class
  • Function
  • Method
  • Table

The question is, when are these properly used? Such as in the example below:

addon = { }
function addon:test_func( )
    return 'hi'
end

Q: From my understanding with Lua and OOP, addon is a table, however, I've read that it can be an object as well -- but when it is technically an object? After a function is created within that table?

Q: test_func is a function, however, I've read that it becomes a "Method" when it's placed within a table (class).

Q: The entire line addon:test_func( ), I know the colon is an operator, but what is the term for the entire line set of text? A class itself?

Finally, for this example code:

function addon:test_func( id, name )

end

Q: What is id and name, because I've seen some people identify them as arguments, but then other areas classify them as parameters, so I've stuck with parameters.

So in short, what is the proper terminology for each of these, and when do they become what they are?

Thanks

1 Answer 1

1

From my understanding with Lua and OOP, addon is a table, however, I've read that it can be an object as well -- but when it is technically an object? After a function is created within that table?

Object is not a well-defined term. I've seen it defined (in C) as any value whatsoever. In Lua, I would consider it synonymous with a table. You could also define it as an instance of a class.

test_func is a function, however, I've read that it becomes a "Method" when it's placed within a table (class).

You're basically right. A method is any function that is intended to be called with the colon notation. Metamethods are also methods, because, like regular methods, they define the behavior of tables.

The entire line addon:test_func( ), I know the colon is an operator, but what is the term for the entire line set of text? A class itself?

There's no name for that particular piece of code. It's just part of a method definition.

Also, I wouldn't call the colon an operator. An operator would be the plus in x + y where x and y both mean something by themselves. In addon:test_func(), test_func only has meaning inside the table addon, and it's only valid to use the colon when calling or defining methods. The colon is actually a form of syntactic sugar where the real operator is the indexing operator: []. Assuming that you're calling the method, the expansion would be: addon['test_func'](addon).

What is id and name, because I've seen some people identify them as arguments, but then other areas classify them as parameters, so I've stuck with parameters.

They're parameters. Parameters are the names that you declare in the function signature. Arguments are the values that you pass to a function.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks! So an "object" could classify anything, and doesn't necessarily get restricted to one particular type. Does the terminology change if you were to use addon.test_func( ) vs addon:test_func( )? All I know is that with a colon, self is passed as the first parameter.
Since Lua is very flexible, it's hard to strictly define what a method is. If test_func is supposed to define a table's behavior (i.e. you intend to call it with a colon), it's a method. If addon is nothing more than storage space for the function, it's not a method.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.