How to create a function with variable parameters just like table.insert()?











up vote
0
down vote

favorite












I know I can use function(a, b, ...) to solve variable parameters problems. But how can I do if I want to create a function like table.insert (table, [pos,] value) ?
Of cause, not in the following way:



function (table, pos, value)
if value == nil then
pos = value
value = nil
end
-- do something
end









share|improve this question







New contributor




Ishanks is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
















  • 3




    Why not in the way above?
    – lhf
    yesterday















up vote
0
down vote

favorite












I know I can use function(a, b, ...) to solve variable parameters problems. But how can I do if I want to create a function like table.insert (table, [pos,] value) ?
Of cause, not in the following way:



function (table, pos, value)
if value == nil then
pos = value
value = nil
end
-- do something
end









share|improve this question







New contributor




Ishanks is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
















  • 3




    Why not in the way above?
    – lhf
    yesterday













up vote
0
down vote

favorite









up vote
0
down vote

favorite











I know I can use function(a, b, ...) to solve variable parameters problems. But how can I do if I want to create a function like table.insert (table, [pos,] value) ?
Of cause, not in the following way:



function (table, pos, value)
if value == nil then
pos = value
value = nil
end
-- do something
end









share|improve this question







New contributor




Ishanks is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











I know I can use function(a, b, ...) to solve variable parameters problems. But how can I do if I want to create a function like table.insert (table, [pos,] value) ?
Of cause, not in the following way:



function (table, pos, value)
if value == nil then
pos = value
value = nil
end
-- do something
end






lua lua-table






share|improve this question







New contributor




Ishanks is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











share|improve this question







New contributor




Ishanks is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









share|improve this question




share|improve this question






New contributor




Ishanks is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









asked yesterday









Ishanks

1




1




New contributor




Ishanks is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





New contributor





Ishanks is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






Ishanks is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.








  • 3




    Why not in the way above?
    – lhf
    yesterday














  • 3




    Why not in the way above?
    – lhf
    yesterday








3




3




Why not in the way above?
– lhf
yesterday




Why not in the way above?
– lhf
yesterday












1 Answer
1






active

oldest

votes

















up vote
0
down vote













Lua does not have function overloading or typechecking of arguments, if needed those should be implemented by the one needing.
Conveying the usage of your function is also up to you.



If you are not satisfied with your provided snippet, you can rewrite it in other manners for example :



function(arg1,arg2,arg3)
tab=arg1
if not arg3 then
value=arg2
index=#tab+1
else
assert(type(arg2)=='numer',"bad argument #2 to 'insert' (number expected, got table)")
value=arg3
index=arg2
end
table.insert(tab,index,value)
end


or:



f2=function(a,b)
--do smth
end
f3=function(a,b,c) end
f=function(...)
args={...}
nargin=#args
if nargin==2 then
f2(args[1],args[2]) --one way to use varied arguments
elseif nargin==3 then
f3(...) --other one
else error("wrong number of arguments")
end
end


I don't recommend you making functions with optional middle positional arguments though. At least not in a language without compile time type check, and even in those as well.



If you really want optional arguments, put them in the table:



function(args)
tab=args.table
index=args.index or #tab+1
value=args.value
--other arguments, options and associated logic here
table.insert(tab,index,value)
end





share|improve this answer





















  • thx,someone asked me about the usage of table.insert() but thought there was something wrong that the variable parameter was in the middle. So I just wonder if there is a special usage for variable parameters.
    – Ishanks
    yesterday











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});






Ishanks is a new contributor. Be nice, and check out our Code of Conduct.










 

draft saved


draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53409313%2fhow-to-create-a-function-with-variable-parameters-just-like-table-insert%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
0
down vote













Lua does not have function overloading or typechecking of arguments, if needed those should be implemented by the one needing.
Conveying the usage of your function is also up to you.



If you are not satisfied with your provided snippet, you can rewrite it in other manners for example :



function(arg1,arg2,arg3)
tab=arg1
if not arg3 then
value=arg2
index=#tab+1
else
assert(type(arg2)=='numer',"bad argument #2 to 'insert' (number expected, got table)")
value=arg3
index=arg2
end
table.insert(tab,index,value)
end


or:



f2=function(a,b)
--do smth
end
f3=function(a,b,c) end
f=function(...)
args={...}
nargin=#args
if nargin==2 then
f2(args[1],args[2]) --one way to use varied arguments
elseif nargin==3 then
f3(...) --other one
else error("wrong number of arguments")
end
end


I don't recommend you making functions with optional middle positional arguments though. At least not in a language without compile time type check, and even in those as well.



If you really want optional arguments, put them in the table:



function(args)
tab=args.table
index=args.index or #tab+1
value=args.value
--other arguments, options and associated logic here
table.insert(tab,index,value)
end





share|improve this answer





















  • thx,someone asked me about the usage of table.insert() but thought there was something wrong that the variable parameter was in the middle. So I just wonder if there is a special usage for variable parameters.
    – Ishanks
    yesterday















up vote
0
down vote













Lua does not have function overloading or typechecking of arguments, if needed those should be implemented by the one needing.
Conveying the usage of your function is also up to you.



If you are not satisfied with your provided snippet, you can rewrite it in other manners for example :



function(arg1,arg2,arg3)
tab=arg1
if not arg3 then
value=arg2
index=#tab+1
else
assert(type(arg2)=='numer',"bad argument #2 to 'insert' (number expected, got table)")
value=arg3
index=arg2
end
table.insert(tab,index,value)
end


or:



f2=function(a,b)
--do smth
end
f3=function(a,b,c) end
f=function(...)
args={...}
nargin=#args
if nargin==2 then
f2(args[1],args[2]) --one way to use varied arguments
elseif nargin==3 then
f3(...) --other one
else error("wrong number of arguments")
end
end


I don't recommend you making functions with optional middle positional arguments though. At least not in a language without compile time type check, and even in those as well.



If you really want optional arguments, put them in the table:



function(args)
tab=args.table
index=args.index or #tab+1
value=args.value
--other arguments, options and associated logic here
table.insert(tab,index,value)
end





share|improve this answer





















  • thx,someone asked me about the usage of table.insert() but thought there was something wrong that the variable parameter was in the middle. So I just wonder if there is a special usage for variable parameters.
    – Ishanks
    yesterday













up vote
0
down vote










up vote
0
down vote









Lua does not have function overloading or typechecking of arguments, if needed those should be implemented by the one needing.
Conveying the usage of your function is also up to you.



If you are not satisfied with your provided snippet, you can rewrite it in other manners for example :



function(arg1,arg2,arg3)
tab=arg1
if not arg3 then
value=arg2
index=#tab+1
else
assert(type(arg2)=='numer',"bad argument #2 to 'insert' (number expected, got table)")
value=arg3
index=arg2
end
table.insert(tab,index,value)
end


or:



f2=function(a,b)
--do smth
end
f3=function(a,b,c) end
f=function(...)
args={...}
nargin=#args
if nargin==2 then
f2(args[1],args[2]) --one way to use varied arguments
elseif nargin==3 then
f3(...) --other one
else error("wrong number of arguments")
end
end


I don't recommend you making functions with optional middle positional arguments though. At least not in a language without compile time type check, and even in those as well.



If you really want optional arguments, put them in the table:



function(args)
tab=args.table
index=args.index or #tab+1
value=args.value
--other arguments, options and associated logic here
table.insert(tab,index,value)
end





share|improve this answer












Lua does not have function overloading or typechecking of arguments, if needed those should be implemented by the one needing.
Conveying the usage of your function is also up to you.



If you are not satisfied with your provided snippet, you can rewrite it in other manners for example :



function(arg1,arg2,arg3)
tab=arg1
if not arg3 then
value=arg2
index=#tab+1
else
assert(type(arg2)=='numer',"bad argument #2 to 'insert' (number expected, got table)")
value=arg3
index=arg2
end
table.insert(tab,index,value)
end


or:



f2=function(a,b)
--do smth
end
f3=function(a,b,c) end
f=function(...)
args={...}
nargin=#args
if nargin==2 then
f2(args[1],args[2]) --one way to use varied arguments
elseif nargin==3 then
f3(...) --other one
else error("wrong number of arguments")
end
end


I don't recommend you making functions with optional middle positional arguments though. At least not in a language without compile time type check, and even in those as well.



If you really want optional arguments, put them in the table:



function(args)
tab=args.table
index=args.index or #tab+1
value=args.value
--other arguments, options and associated logic here
table.insert(tab,index,value)
end






share|improve this answer












share|improve this answer



share|improve this answer










answered yesterday









Dimitry

8871516




8871516












  • thx,someone asked me about the usage of table.insert() but thought there was something wrong that the variable parameter was in the middle. So I just wonder if there is a special usage for variable parameters.
    – Ishanks
    yesterday


















  • thx,someone asked me about the usage of table.insert() but thought there was something wrong that the variable parameter was in the middle. So I just wonder if there is a special usage for variable parameters.
    – Ishanks
    yesterday
















thx,someone asked me about the usage of table.insert() but thought there was something wrong that the variable parameter was in the middle. So I just wonder if there is a special usage for variable parameters.
– Ishanks
yesterday




thx,someone asked me about the usage of table.insert() but thought there was something wrong that the variable parameter was in the middle. So I just wonder if there is a special usage for variable parameters.
– Ishanks
yesterday










Ishanks is a new contributor. Be nice, and check out our Code of Conduct.










 

draft saved


draft discarded


















Ishanks is a new contributor. Be nice, and check out our Code of Conduct.













Ishanks is a new contributor. Be nice, and check out our Code of Conduct.












Ishanks is a new contributor. Be nice, and check out our Code of Conduct.















 


draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53409313%2fhow-to-create-a-function-with-variable-parameters-just-like-table-insert%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks

Calculate evaluation metrics using cross_val_predict sklearn

Insert data from modal to MySQL (multiple modal on website)