Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit faa44ef

Browse files
committed
overloading: Lua (version 2)
1 parent a85fcc6 commit faa44ef

File tree

3 files changed

+26
-8
lines changed

3 files changed

+26
-8
lines changed

‎book/cite.bib‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -586,4 +586,13 @@ @online{2009-bailey
586586
url = {https://stackoverflow.com/a/1734905},
587587
urldate = {Oct 6, 2009},
588588
organization = {stackoverflow.com},
589+
}
590+
591+
@online{2011-berteig,
592+
author = {{RBerteig of stackoverflow}},
593+
title = {Variable number of function arguments Lua 5.1},
594+
year = {2011},
595+
url = {https://stackoverflow.com/a/7630202},
596+
urldate = {Oct 3, 2011},
597+
organization = {stackoverflow.com},
589598
}

‎book/polymorphism.tex‎

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -481,11 +481,14 @@ \subsection{Javascript}
481481
\subsection{Lua}
482482

483483
Lua does not support method overloading.
484-
A similar behavior can be achieved by testing arguments number and types.
484+
A similar behavior can be achieved by using a \nameword{variadic function} and testing arguments number and types.
485+
You can select a parameter using the function \keyword[Lua]{select} \citep{2011-berteig}.
486+
The same function can be used to know te number of parameters.
485487

486-
\lstinputlisting[language={[KB]Lua}, linerange={2-19,35-37}, style=codeStyle]{../codes/lua/polymorphism/overloading.lua}
488+
\lstinputlisting[language={[KB]Lua}, linerange={2-20,37-39}, style=codeStyle]{../codes/lua/polymorphism/overloading.lua}
487489

488490
Subclasses can define the same method, verify the arguments for new definitions and delegate the control to the super's methods.
491+
If you do not want to use \keyword[Lua]{select}, you can put the arguments in a list and access the parameters as list members.
489492

490493
\lstinputlisting[language={[KB]Lua}, linerange={22-32,40-43}, style=codeStyle]{../codes/lua/polymorphism/overloading.lua}
491494

@@ -502,6 +505,10 @@ \subsection{Perl}
502505

503506
\lstinputlisting[language={[KB]Perl}, linerange={13-25,34-37}, style=codeStyle]{../codes/perl/polymorphism/overloading.pl}
504507

508+
\subsection{PHP}
509+
510+
PHP does not support method overloading; Overloading in PHP is entirely a different thing.
511+
505512

506513
\section{Methods overriding}
507514

‎codes/lua/polymorphism/overloading.lua‎

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,9 @@ function Person:new() -- The constructor
88
return instance
99
end
1010

11-
function Person:read(arg1)
12-
if not arg1 then
11+
function Person:read(...)
12+
local arg1 = select(1, ...)
13+
if not arg1 then -- select("#", ...) for parameters number
1314
print("I am reading")
1415
else
1516
if type(arg1) == "string" then
@@ -23,11 +24,12 @@ local Student = {}
2324
Student.__index = Student
2425
setmetatable(Student, Person) -- extends
2526

26-
function Student:read(arg1)
27-
if type(arg1) == "number" then
28-
print("I read on table n°: " .. arg1)
27+
function Student:read(...)
28+
local args = {...}
29+
if type(args[1]) == "number" then
30+
print("I read on table n°: " .. args[1])
2931
else
30-
Person.read(self, arg1)
32+
Person.read(self, ...)
3133
end
3234
end
3335

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /