Adam Cameron
Adam Cameron
function f(...rest){
console.log(rest)
}
f() // []
f(1) // [1] <------ AN ARRAY
f(1,2) // [1,2]
f(1,2,3) // [1,2,3]
PHP:
function f(...$rest){
var_dump($rest);
}
f(); // []
f(1); // [1] <------ AN ARRAY
f(1,2); // [1,2]
f(1,2,3); // [1,2,3]
Ruby:
require 'json'
def f (*rest)
puts rest.to_json
end
f # []
f(1) # [1] <------ AN ARRAY
f(1,2) # [1,2]
f(1,2,3) # [1,2,3]
ColdFusion:
function f(...rest){
writeDump(rest)
}
//f() // error
f(1) // 1 <------ NOT AN ARRAY
f(1, 2) // [1, 2]
f(1, 2, 3) // [1, 2, 3]
Clearly wasn't tested.bdw429s
05/09/2022, 6:04 PMAdam Cameron
f(...x)
is of almost zero use (or actually zero?), but there's a case for f(x,y,...z)
where x
and y
have specific meaning, and there's also a case for "and anything else you give will be treated as a collection called z
"
But it's largely syntactic sugar.bdw429s
05/09/2022, 6:12 PMf(x,y,...z)
-- when I find myself doing this, I'm just allowing the arguments scope itself to be the array and I'm building a DSL like
watch()
.paths( 'models', 'views', 'handlers' );
Adam Cameron
printf("String: %s, int: %i, float: %f", "one", 2, 3.0)
. I think it could be argued that's more natural than printf("String: %s, int: %i, float: %f", ["one", 2, 3.0])
. Albeit not by much. And it's not like the latter is even bad.bdw429s
05/09/2022, 6:13 PMAdam Cameron
Daniel Mejia
05/09/2022, 7:15 PM