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
lua lua-table
New contributor
add a comment |
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
lua lua-table
New contributor
3
Why not in the way above?
– lhf
yesterday
add a comment |
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
lua lua-table
New contributor
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
lua lua-table
New contributor
New contributor
New contributor
asked yesterday
Ishanks
1
1
New contributor
New contributor
3
Why not in the way above?
– lhf
yesterday
add a comment |
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
add a comment |
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
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
add a comment |
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
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
add a comment |
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
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
add a comment |
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
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
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
add a comment |
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
add a comment |
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.
Ishanks is a new contributor. Be nice, and check out our Code of Conduct.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
3
Why not in the way above?
– lhf
yesterday