Thread: table behaviour
View Single Post
09/23/20, 05:05 AM   #5
Baertram
Super Moderator
 
Baertram's Avatar
WoWInterface Super Mod
AddOn Author - Click to view addons
Join Date: Mar 2014
Posts: 4,989
First of all you should decide either using . or :, but not a mixture (where not needed).

:
: is used for the kind of object-oriented approach where you define a "class" and create objects of that class, and then you call functions of that object referencing it via self.
-> Where the "class" is only a table which the objects use via metatables to "inherit the functions and attributes of the parent class table".

Classes in ESO lua are defined using ZO_Object:Subclass() e.g. like this:
Lua Code:
  1. local ZO_PlatformStyleManager = ZO_Object:Subclass()
  2.  
  3. function ZO_PlatformStyleManager:New()
  4.     local obj = ZO_Object.New(self)
  5.     obj:Initialize()
  6.     return obj
  7. end

ZO_PlatformStyleManager is the "class", and
this function creates an object "PLATFORM_STYLE_MANAGER " of that class:
Lua Code:
  1. local PLATFORM_STYLE_MANAGER = ZO_PlatformStyleManager:New()

objects using the : to call functions are able to use the variable "self" in their function code to reference the object variable.
Lua Code:
  1. function ZO_PlatformStyleManager:Add(object)
  2.     table.insert(self.objects, object)
  3. end
self.objects will be the table "objects" at self, where self is the created object of class ZO_PlatformStyleManager.

As the created object of ZO_PlatformStyleManager is PLATFORM_STYLE_MANAGER (See above), self.objects will be PLATFORM_STYLE_MANAGER .objects then.


If you do not plan to create a class and have an object, or even multiple objects (like a logger library/addon creates multiple loggers with base function ":log" [defined at the super class] e.g., but different functions for each logger e.g. :warning, :error [defined at each individual logger]) you shouldn't use :

That's my opinion so feel free to do like you wish and prefer


.

. just is using a "table", no object-oriented approach, no metatables.

You define a table
Lua Code:
  1. addonName = {}
and define variables, subtables, function and assign it to the table using the .
Lua Code:
  1. addonName.subTable = {}
  2. addonName.var1 = "Hello world"
  3. function addonName.myFunc(param1) end

variables using the . to call functions are not able to use the variable "self" in their function code to reference the table variable.
If you want to specify any object/table you reference to, you need to name it by it's name.

Lua Code:
  1. function addonName.myFunc(param1)
  2.  --self is not given here! If you want to reference variables), just use the variable name
  3.  
  4. end

The object example function "ZO_PlatformStyleManager:Add(object)" from above, using the :, would look like this using the . instead:
Lua Code:
  1. function ZO_PlatformStyleManager.Add(objectVariableOfClass, object)
  2.     table.insert(objectVariableOfClass.objects, object)
  3. end
The 1st parameter of the function would be the created object of the class, and you cannot use self anymore, but need to use this passed in parameter then.
The call to the function would not be this anymore:
Lua Code:
  1. local CreatedObjectOfZO_PlatformStyle = ZO_PlatformStyle:New(param1, param2, ...)
  2. PLATFORM_STYLE_MANAGER:Add(CreatedObjectOfZO_PlatformStyle)
But this:
Lua Code:
  1. local CreatedObjectOfZO_PlatformStyle = ZO_PlatformStyle:New(param1, param2, ...)
  2. PLATFORM_STYLE_MANAGER.Add(PLATFORM_STYLE_MANAGER, CreatedObjectOfZO_PlatformStyle)


Again this is only my opinion:
You should stick to using the . way and normal tables to keep it easy, where possible.
But this is totally up to you. You can create your addon as a class + object, or even without deifning it as a class (ZO_Object:Subclass()) you ca still use the : .
But it does not make that much sense, or at least I did not get why it would so far

Last edited by Baertram : 09/23/20 at 05:13 AM.
  Reply With Quote