2

This is more of a design philosophy question as I already know you shouldn't call a function with : (object-oriented syntactic sugar) if the function has been defined without the self keyword by using .. But the problem is that programmers using a library I have created tend to not read the documentation and run into the question of "how should I call your function?", so I end up always defining functions using the method below:

local tbl = {};
function tbl:Add(a, b)
   return a + b;
end

I have installed Luacheck (in VS Code) and it often complains when I use this syntax and not use the self referential keyword. It says: [luacheck] unused argument "self". Is there any problem with this in terms of performance (or is there a way of disabling Luacheck in VS Code)?

I prefer writing functions in this style as opposed to the style below:

function tbl.Add(_, a, b)
    return a + b;
end

It seems a pain to have to add a dummy variable at the start of the parameter list.

EDIT: Another problem is what if you had many tables that implement a function with the same name and want to iterate over them but some implementations do not use the self argument and others do? It would be very tedious and bad design to check what type of table it is to call the function correctly.

What is the preferred style? A bit confused in general about this warning. What are your thoughts? Thanks.

Mayron
  • 2,146
  • 4
  • 25
  • 51
  • 1
    `:` for object methods and class methods, `.` for static members. – Egor Skriptunoff Dec 07 '18 at 12:50
  • @EgorSkriptunoff thanks. I wish there was a more expressive way to tell the user "this is a static method! Do NOT use `:`!" Because it seems like a common bug people have when calling functions in my library. I could do `tbl.static.Add()` but that seems wasteful and ugly. I'll have to stick with documentation and pray they read it... – Mayron Dec 07 '18 at 12:55
  • 1
    The symbol `.`/`:` is part of a method signature and must be specified in the documentation. People have anyway to read the documentation to learn which arguments the function expects. – Egor Skriptunoff Dec 07 '18 at 13:40
  • @EgorSkriptunoff Another example is if you expect many tables to implement a function with a given name (such as subclasses inheriting from the same parent class using metatables) and you want to iterate over all these tables and call this function (using the same function name), then some might use `self` and others might not. This is a good example of why I think I should ignore this luacheck warning. I often think it is safer to just ignore this. – Mayron 10 mins ago – Mayron Dec 07 '18 at 14:28
  • From a Lua code point of view, nothing but the entire source code can express to the user of a function what the author intended. Documentation must be written and read. This should be obvious for the return(s) lists but is also the case for calling as a method nor not. All in all, the Principle of Least Astonishment applies. So for a reasonably designed library, documentation would focus on domain issues rather than implementation minutia and over-cleverness. (In the general sense, definition as a method or not is independent of calling as a method or not.) – Tom Blodget Dec 07 '18 at 23:44

1 Answers1

1

if you're not using the self argument you can just do

function tbl.Add(a, b)
    return a + b;
end

no need to use a dummy variable.

You just need to be sure then that you also call it with a . and not a :
so

local someValue = tbl.Add(1, 3)

for example and not

local someValue = tbl:Add(1, 3)
Ivo
  • 18,659
  • 2
  • 23
  • 35
  • The problem I have is that I am using creating a shared library and I think expecting the user/programmer to call the methods/functions correctly makes the library harder to use as it relies on them to read the code directly, or the documentation carefully. – Mayron Dec 07 '18 at 12:42