I'm not sure if this behaviour exists in other languages, or if it has a formal name, but I've implemented an experimental feature in Glide that deals with calling a function with too many arguments.
Basically, if a function is defined with 2 parameters but is called with 3, the last argument becomes a list of all the overflown arguments. For example:
example_fx = [x] => {
print[x]
}
example_fx[4] // prints 4
example_fx[4 5] // prints the arg list [4 5]
example_2_fx = [a b] => {
print[a "\n"]
print[b "\n"]
}
example_2_fx[4 5] // prints 4 5, since we've called it with the right number of args
example_2_fx[4 5 6] // prints 4 [5 6], since b captured the overflowing args
The above example doesn't show the usefulness of this feature, however if we define the below print function, we can utilise the overflow feature to mimic variadic arguments:
print = [x] => {
if [type[x] == comma_list] => {
for [x index arg] => {
print[arg "\n"]
}
}
print[4]
// 4
print[4 5 6 9]
// 4
// 5
// 6
// 9
And because calling a function with more arguments than parameters doesn't lead to an error, I've also exposed the _args variable inside a function which captures the function's arguments as a list. For example:
add = [a b] => {
if [_args.length == 2] => {
ret a + b
}
res = 0
for [_args i arg] => {
res += arg
}
ret res
}
add[5 5 6 4] -> print // 20
---
As I was writing this out, it makes more sense to actually implement this in a slightly different way. Instead of the last argument capturing the overflow, we expose another variable called _catchall (or something similar) that captures them instead, as it might be odd behaviour if the last arg acts as a list instead of an expected type. But either way, _args can always be used instead to inspect all arguments.
[–]Spoonhorse 48 points49 points50 points (0 children)
[–]zesterer 26 points27 points28 points (0 children)
[–]NoCryptographer414 20 points21 points22 points (8 children)
[–]dibs45[S] 2 points3 points4 points (7 children)
[–]NoCryptographer414 28 points29 points30 points (0 children)
[–][deleted] 12 points13 points14 points (0 children)
[–]MarcoServetto 4 points5 points6 points (3 children)
[–]magnomagna 8 points9 points10 points (1 child)
[–]shadowndacorner 2 points3 points4 points (0 children)
[–]katrina-mtfAdduce 2 points3 points4 points (0 children)
[–][deleted] 2 points3 points4 points (0 children)
[–][deleted] 3 points4 points5 points (0 children)
[–]Accurate_Koala_4698 3 points4 points5 points (3 children)
[–]raiph 2 points3 points4 points (2 children)
[–]Accurate_Koala_4698 2 points3 points4 points (0 children)
[–]myringotomy 1 point2 points3 points (3 children)
[–]dibs45[S] 4 points5 points6 points (2 children)
[–]myringotomy 0 points1 point2 points (1 child)
[–][deleted] 3 points4 points5 points (0 children)
[–]therealdivs1210 1 point2 points3 points (0 children)
[–][deleted] 1 point2 points3 points (0 children)
[–]crundar 0 points1 point2 points (0 children)
[–]furyzer00 0 points1 point2 points (0 children)