Thread Tools Display Modes
09-05-16, 01:53 AM   #1
Layback_
An Onyxian Warder
Join Date: Feb 2016
Posts: 358
Function Overloading?

Since functions in Lua does not have its parameter types defined from function declaration, this could be not an ideal to utilize the concept of 'Function Overloading'.

However, according to this page, you could still overload functions in Lua as well (but not a straight-forward approach like that is in C++ or Java).

I'm asking this because of :SetPoint(...) function.

On Wowwiki, it introduces four different ways to call :SetPoint(...) method which are:

Lua Code:
  1. obj:SetPoint(point, relativeFrame, relativePoint, ofsx, ofsy);
  2. obj:SetPoint(point, relativeFrame, relativePoint);
  3. obj:SetPoint(point, ofsx, ofsy);
  4. obj:SetPoint(point);

As we all know, data type for 2nd and 3rd approach are totally different (apart from 'point' i mean).

So, my question is that does anyone here know whether this is done via fucntion overloading approach or via some other method like variable type checking?

e.g.
Lua Code:
  1. if type(arg2) == "number" then
  2.     ...
  3. elseif type(arg2) == "string" then
  4.     ...
  5. end

Last edited by Layback_ : 09-05-16 at 01:58 AM.
  Reply With Quote
09-05-16, 05:43 AM   #2
Resike
A Pyroguard Emberseer
AddOn Author - Click to view addons
Join Date: Mar 2010
Posts: 1,290
You can get the number of incoming arguments like this:

Lua Code:
  1. function Legion(...)
  2.     local argnum = select("#", ...)
  3.     if argnum  == 5 then
  4.  
  5.     elseif argnum == 4 then
  6.  
  7.     else
  8.  
  9.     end
  10. end
  Reply With Quote
09-05-16, 04:08 PM   #3
Layback_
An Onyxian Warder
Join Date: Feb 2016
Posts: 358
Originally Posted by Resike View Post
You can get the number of incoming arguments like this:

Lua Code:
  1. function Legion(...)
  2.     local argnum = select("#", ...)
  3.     if argnum  == 5 then
  4.  
  5.     elseif argnum == 4 then
  6.  
  7.     else
  8.  
  9.     end
  10. end
Hi Resike,

Yeah I'm aware of that

But is :SetPoint method also written that way?
  Reply With Quote
09-05-16, 07:37 PM   #4
Resike
A Pyroguard Emberseer
AddOn Author - Click to view addons
Join Date: Mar 2010
Posts: 1,290
Originally Posted by Layback_ View Post
Hi Resike,

Yeah I'm aware of that

But is :SetPoint method also written that way?
I'm not sure, but i wouldn't be surprised. And unless you want to do something really funky thats the easiest method.

Last edited by Resike : 09-05-16 at 07:42 PM.
  Reply With Quote
09-05-16, 08:40 PM   #5
lightspark
A Rage Talon Dragon Guard
 
lightspark's Avatar
AddOn Author - Click to view addons
Join Date: Sep 2012
Posts: 341
Originally Posted by Layback_ View Post
Hi Resike,

Yeah I'm aware of that

But is :SetPoint method also written that way?
No, cuz it's implemented in C++, probably, I'm mostly certain it's
__________________

Last edited by lightspark : 09-05-16 at 09:17 PM.
  Reply With Quote
09-06-16, 02:16 PM   #6
SDPhantom
A Pyroguard Emberseer
 
SDPhantom's Avatar
AddOn Author - Click to view addons
Join Date: Jul 2006
Posts: 2,326
C API functions are written a lot different than Lua functions. A C function gets the Lua state as its only argument. C-side functions can be called on the state to see how many arguments were passed from the Lua engine and pop the values from the stack. The function is expected to push returns to the stack and return the number of values that were pushed.

This example is in the lua_CFunction section of the Lua 5.1 reference manual.
C Code:
  1. static int foo (lua_State *L) {
  2.     int n = lua_gettop(L);  /* number of arguments */
  3.     lua_Number sum = 0;
  4.     int i;
  5.     for (i = 1; i <= n; i++) {
  6.         if (!lua_isnumber(L, i)) {
  7.             lua_pushstring(L, "incorrect argument");
  8.             lua_error(L);
  9.         }
  10.         sum += lua_tonumber(L, i);
  11.     }
  12.     lua_pushnumber(L, sum/n);   /* first result */
  13.     lua_pushnumber(L, sum);     /* second result */
  14.     return 2;                   /* number of results */
  15. }
__________________
WoWInterface AddOns
"All I want is a pretty girl, a decent meal, and the right to shoot lightning at fools."
-Anders (Dragon Age: Origins - Awakening)
  Reply With Quote
09-06-16, 04:46 PM   #7
Layback_
An Onyxian Warder
Join Date: Feb 2016
Posts: 358
Many thanks to Resike, lightspark and especially to SDPhantom

Did not really give an attention to reference manual page, but looks interesting!

I'll probably use the method that Resike suggested as I also think that would be the easiest way

Last edited by Layback_ : 09-06-16 at 04:54 PM.
  Reply With Quote
09-06-16, 09:42 PM   #8
Phanx
Cat.
 
Phanx's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2006
Posts: 5,617
Originally Posted by Resike View Post
Lua Code:
  1. function Legion(...)
  2.     local argnum = select("#", ...)
  3.     if argnum  == 5 then
  4.  
  5.     elseif argnum == 4 then
  6.  
  7.     else
  8.  
  9.     end
  10. end
Another way to do this would be to just assign all the parameters to variables (up to the max accepted) and then check if they exist. Here's an example of how you might handle the current functionality of SetPoint in pure Lua:

Lua Code:
  1. function Frame:SetPoint(point, relTo, relPoint, x, y)
  2.     if y then
  3.         -- "TOPLEFT", UIParent, "CENTER", 5, 20
  4.     elseif x then
  5.         -- "TOPLEFT", UIParent, 5, 20
  6.         relPoint, x, y = point, relPoint, x
  7.     elseif relPoint then
  8.         if type(relTo) == "number" then
  9.             -- "TOPLEFT", 5, 20
  10.             relTo, relPoint, x, y = self:GetParent(), point, relTo, relPoint
  11.         else
  12.             -- "TOPLEFT", UIParent, "CENTER"
  13.             x, y = 0, 0
  14.         end
  15.     elseif relTo then
  16.         -- "TOPLEFT", UIParent
  17.         relPoint, x, y = point, 0, 0
  18.     elseif point then
  19.         -- "TOPLEFT"
  20.         relTo, relPoint, x, y = self:GetParent() point, 0, 0
  21.     end
  22.     -- Now you have all the expected arguments
  23.     -- and can do whatever you want with them.
  24. end

In practice you'd also want additional sanity checks for missing values in the middle of the list (not sure what the real SetPoint actually does if you pass something like "TOPLEFT", nil, "CENTER") and value types (x/y need to be numbers, so use tonumber and fall back to 0; relTo needs to be a frame reference, so look up strings in _G and fall back to the frame's parent if you can't resolve it to a frame; etc).
__________________
Retired author of too many addons.
Message me if you're interested in taking over one of my addons.
Don’t message me about addon bugs or programming questions.
  Reply With Quote

WoWInterface » Developer Discussions » Lua/XML Help » Function Overloading?


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off