❔ Repeating parameters vs variadic functions
Does anybody know why the
params
keyword is not used more commonly? This example if from the ImmutableArray<>
class. As you can see there's methods for creating an array from 1-4 args, and then it goes to an array arg, but the array method is then using the params
keyword? Why not just use the params
keyword on the first method so that it could take N args without needing multiple methods with differing amounts of parameters? I feel like I'm missing something here.9 Replies
Don't quote me but iirc arrays are stored in a different memory that is a lot more expensive than passing simple values as parameters
In practice it's unoticable, programs use arrays all the time
This is probably a good chance for me to do a little benchmarking and see what the performance difference between the two method signatures
But it can make a difference in performance critical situations
Yes, it's for performance
Much cheaper to pass 3 parameters than to make an array
Benchmark that demonstrates Fred's point. The One through Four have the explicit definitions from above, five is where it becomes variadic
Turn on memory benchmarking too
I saw an issue on span params, this should be resolved when they add those
Was this issue resolved? If so, run
/close
- otherwise I will mark this as stale and this post will be archived until there is new activity.