Method declaration in Lua - function

Is there any difference between these two types of declarations performance-wise?
local object = newObject()
function object:method(params)
end
local object:method = function(params)
end

Yes, there is a difference. The second one doesn't compile. So it has zero performance ;)
A "method declaration" is just syntactical sugar in Lua. These are identical:
function object.func(self, param)
end
function object:func(param)
end
But that sugar only works if you are naming the function as part of the function declaration.
The ':' syntax for accessing "methods" in Lua only works for accessing functions that are stored in a table, named by a string key. You cannot use this syntax to set the value of a table.
Or, to put it another way, there is no other way to do this:
function object:func(param)
end
without explicitly specifying a 'self' parameter as the first parameter.

Related

Lua - Should I use ":" when defining functions in tables if not using the "self" keyword?

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.
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)

In Lua, what is the difference between functions that use ":" and functions that do not? [duplicate]

This question already has answers here:
Difference between . and : in Lua
(3 answers)
Closed 7 years ago.
Let's say we have two function declarations:
function MyData:New
end
and
function New(MyData)
end
What is the difference between them? Does using : have any special purpose when it comes to inheritance and OOP? Can I only call functions declared with : by using :?
I'm coming from using only C# -- so if there's any comparison to be made, what would it be?
Adapted from the manual, end of ยง3.4.10:
The colon syntax is used for defining methods, that is, functions that have an implicit extra parameter self. Thus, the statement
function t:f (params) body end
is syntactic sugar for
t.f = function (self, params) body end
You should search SO as there are many questions about this but you have a set of questions so I can't say this is a duplicate.
Q. What is the difference between them?
A. The one with colon causes a method to be added to the MyData table, and the Lua interpreter to automatically insert a "self" before the first parameter when called, with this "self" pointing to the MyData instance that the "method" is being called upon. It is the same as writing MyData.New = function(self) end. The second signature has a parameter called MyData and is a global function. It is unrelated to the MyData table (or class).
Q. Does using ":" have any special purpose when it comes to inheritance and OOP?
A. No; it is merely syntactic sugar so that when you call MyData.New you can just write MyData:New() instead of the clunky looking MyData.New(MyData). Note that a "new" function is typically to create instances of a class, so you wouldn't have such a function be a method, rather just a function in the MyData table. For inheritence and OOP, you use metatables, and this does not interact with : in any special way.
Q. Can I only call functions declared with ":" by using ":"?
A. No, as mentioned, it just syntactic sugar, you can define one way and call a different way.
Q. I'm coming from using only C# -- so if there's any comparison to be made, what would it be?
A. For functions, the : is like the . in C#, whether used in a call or definition. The "." in Lua is more like "attribute", there is no equivalent in C# for functions.
MyData = {} -- a table
function MyData.func(self)
print('hello')
end
MyData:func()
MyData.func(MyData) -- same as previous
function MyData:func2() -- self is implicit
print('hello')
end
MyData:func2()
MyData.func2(MyData) -- same as previous
Note that MyData as defined above is not a class, because you cannot create "instances" of it (without doing extra work not shown there). Definitely read the Programming in Lua online book on the Lua.org website, lots of useful discussions of these notions.
Lua doesn't have functions declarations per se; It has function definition expressions. The syntax you have used is shorthand for a function definition expression and assignment.
The only difference in your examples is when the first statement is executed a new function is created and assigned to the field New in the table referenced by the variable MyData, whereas the second is an assignment to a non-field variable (local, if previously declared, otherwise global).
Keep in mind that these are only the first references to the created function values. Like any other value, you can assign references to functions to any variable and pass them as parameters.
If you add formal parameter usage to the bodies then there is another difference: The first has an implicit first parameter named self.
If you add function calling to the scenarios, the ":" syntax is used with an expression on the left. It should reference a table. The identifier to the right should be a field in that table and it should reference a function. The value of the left expression is passed as the first actual argument to the function with any additional arguments following it.
A function definition with a ":" is called a method. A function call with a ":" is called a method call. You can construct a function call to a function value that is a field in a table with the first argument being a reference to the table using any function call syntax you wish. The Lua method definition and method call syntax makes it easier, as if the function was an instance method. In this way, a Lua method is like a C# Extension Method.

append to function lua

I have a function I want to add to dynamically as the program runs.
Let's say I have function Foo:
function foo()
Function1()
Function2()
Function3()
end
and I want to change Foo() to:
function foo()
Function1()
Function2()
Function3()
Function4()
end
later in the program. Is there any way to do this?
Just do it. The code that you wrote works fine. Functions in Lua can be redefined as desired.
If you don't know what foo does, you can do this:
do
local old = foo
foo = function () old() Function4() end
end
Or perhaps it is clearer to use a table of functions:
local F={ Function1, Function2, Function3 }
function foo()
for i=1,#F do F[i]() end
end
Latter, do
F[#F+1]=Function4
and you don't need to redefine foo.
This is a supplementary answer with background information.
Lua identifiers are used for global variables, local variables, parameters and table fields. They hold any type of value.
Lua functions are values. Lua functions are all anonymous, regardless of the syntax used to define them.
function f()
--...
end
is a Lua statement that compiles to a function definition and an assignment to a variable. It's an alternate to
f = function()
--...
end
Each time a function definition is executed, it produces a new function value, which is then used in any associated expression or assignment.
It should be clear that neither statement necessarily creates a new variable nor requires it to always have the same value, nor requires it to always hold a function value. Also, the function value created need not always be held only by the one variable.
It can be copied just like any other value.
Also, just like other values, function values are garbage collected. So, if f had a function value and is assigned a different value or goes out of scope (say, if it wasn't a global variable), the previous value will be garbage collected when nothing else refers to it.
Without any other context for function f() end, we would assume that f is a global variable. But that's not necessarily the case. If f was an in-scope local or parameter, that is the f that would be assigned to.

Functions and procedures in QTP/VBA/VB6

What is the difference between function and procedure ?Aprart from returning value
Because function can also be used as a procedure if you dont return any value then what is the difference...then what is the use of functions ?
Please specify a scenario where we can use functions and procedures ??
Since your title specifies VBA and VB6, I'll reference the type of subroutines used by those languages. VBA and VB6 use "Function" for subroutines that return a value and "Sub" for those that do not. It's certainly possible to use a Function for all of your subroutines and just ignore the return value. Unlike C++ and many other languages, you're not required to return a value from a VB function. Because VB automatically initializes all variables to default values (zero for number types, False for Boolean, the empty string for String, etc), any functions that don't explicitly return a value will simply return their default value, which you can ignore. For example:
Function MyFunc() As Long
' Nothing here
End Function
This function will return the value zero.
So while you can use Function in place of Sub and just ignore the return values, it's not a good programming practice. Other users of your code will assume you chose Function instead of Sub because you intended to return something meaningful and will likely be surprised to discover that you're not returning anything at all!
There MAY also be a slight performance hit when using Function vs Sub, due to the extra parameter value that is passed on the calling stack (the return value).

Ada package function identifier expected

Learning Ada and trying to make a stack ADT and I'm using this webpage to figure it out.
http://www.functionx.com/ada/Lesson06.htm
eightqueens.adb
with Ada.Text_IO;
use Ada.Text_IO;
with Stack;
use Stack;
procedure EightQueens is
begin
put_line ("awd");
end EightQueens;
stack.ads
package Stack is
function awd () return Integer;
end Stack;
stack.adb
package body Stack is
function awd () return integer is
begin
return 1;
end awd;
end Stack;
Error is
stack.ads:2:19: identifier expected
I'm most certain I did everything correctly.
Ada doesn't use empty parentheses, either for defining or for calling functions or procedures.
And for future reference, the phrase "I'm most certain I did everything correctly." is a red flag indicating that you've almost certainly done something wrong.
Just to elaborate, there are some syntactic decisions that Ada made that IMHO are superior to what you may be used to from C-syntax languages.
Functions with no parameters don't use empty parenthesis in their calls. This allows you to change a contant to a function call without having to recode any of the clients.
Arrays use parentheses like function calls do, rather than some unique syntax. This allows you to change an array constant to a function call without having to recode any of the clients.
To look at it another way, a constant is just a simplified version of a parameterless function, for when you can get away with always returning the same value. Likewise, a constant array is a simplified version of a parametered function call, for when you can get away with always returning the same value. If you later discover you need a more complex implementation, that's not the client's concern, and should not affect their code.