Allow spread operator as parameters #2278
Labels
feature
Proposed language feature that solves one or more problems
state-duplicate
This issue or pull request already exists
The proposal
This issue proposes a Dart language feature which takes inspiration from the Java lang spread operator on functions/methods parameters. Having such a feature allows creation of infinity parameters known as
arguments
in order to allow dynamic amounts.Why is needed?
Because some time we don't know how many parameters will be passed, see the following example:
First of all, this is just an example of the usage, I think something like this makes understanding the idea. As evident the number of %s is variable it can be one or none. Under the hood, the
format
method makes use of spread operator on parameters like so:If we need to simulate this method with the actual dart version, we will use list instead, like so:
So, if something like this works, why this proposal? Well, it's because of the syntax, imagine that we have only one argument it doesn't make any sense using a list. The second issue is that we can pass an empty list to convince the method it doesn't make any sense, but it's fine for the compiler. I don't know how the compiler deals with some situation like this, but I feel that passing an empty list is a waste of something.
An overstep
Well if the proposal doesn't convince you then do not waste your time, else this is an extra feature which I would like to see but if not, it doesn't make me crazy at all.
Extending the spread operator to generics types, what this does mean this is: allowing something like having infinite parameters in methods, see following example:
Why have something like this?
Well, if any one of you usede provider package in the flutter world, for sure he/she saw code like this:
I think you can imagine how much time and energy you can save with this approach, of course some rule cannot be broken like:
All Ts are generic, but if you are using
extends
to make this a subtype of any All Ts are the same type.By the end It would be great if we can specify a range or at least the max number of parameters.
Thanks!
I hope I was clear, and I hope you'll like the idea.
The text was updated successfully, but these errors were encountered: