Are we all ready for the next bug in CF's rest ope...
# adobe
a
Are we all ready for the next bug in CF's rest operator implementation?
๐Ÿ‘ 1
๐Ÿ‘๐Ÿพ 1
JS:
Copy code
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:
Copy code
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:
Copy code
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:
Copy code
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.
๐Ÿคจ 1
b
I never really did understand the use case for this in CFML (or at least the pressing need for it) outside of "let's copy some stuff from other languages so we can feel cool". CFML's existing features โ€ข loose typing โ€ข argumentCollection โ€ข ability to pass whatever arguments you want to a function have always done what I needed. I put that 2 cents in on the prerelease and never thought about it again ๐Ÿ˜•
a
The only benefit I see is that it formalises the method signature to make it clearer what the intent is. And this is not a bad thing. I think
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.
b
I think the ability to add types to the arguments is nice. I'm not sure if I've ever actually had a real scenario where I had a mix like
f(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
Copy code
watch()
  .paths( 'models', 'views', 'handlers' );
a
I think the most obvious use case for a variadic function over using a specific arg is like
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.
b
Anywhoo, your observation is correct that the implementation here is a no-go.
โœ… 2
a
(we were typing at the same time then)
d
In december when I was learning python and algorithms. It was nice to also learn that cfml was similar to python and javascript in many syntactical ways. Then next generation of programmers are learning javascript and python(mostly) and all this "cool" stuff just makes CF learning easier.