Skip to content

Allow spread operator as parameters #2278

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
elbeicktalat opened this issue Jun 6, 2022 · 5 comments
Closed

Allow spread operator as parameters #2278

elbeicktalat opened this issue Jun 6, 2022 · 5 comments
Labels
feature Proposed language feature that solves one or more problems state-duplicate This issue or pull request already exists

Comments

@elbeicktalat
Copy link

elbeicktalat commented Jun 6, 2022

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:

//java
String text = 'Hi %s is your age %s? or %s?';
String.format(text, 'Alex', 30, 28);

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:

//java 
// implementation is not needed
format(@NotNull String text, Object... args);

If we need to simulate this method with the actual dart version, we will use list instead, like so:

//dart
// a showcase, dart lang doesn't have such thing
String.format(String s, [List<Object>? args]);

String text = 'Hi %s is your age %s? or %s?';
String.format(text, ['Alex', 30, 28]);

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:

class Car<T, ...Ts> {}

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:

class ChangeNotifierProxyProvider<T,  R extends ChangeNotifier?> {}
class ChangeNotifierProxyProvider2<T, T2, R extends ChangeNotifier?> {}
class ChangeNotifierProxyProvider3<T, T2, T3, R extends ChangeNotifier?> {}
// and so on so of
// Note model is any class
ChangeNotifierProxyProvider3<Model1, Model2, Model3, ChangeNotifier>();

// if this propose can work
class ChangeNotifierProxyProvider<...Ts, R extends ChangeNotifier?> {}
ChangeNotifierProxyProvider<Model1, Model2, Model3, ..... Model20, ChangeNotifier>();

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.

// all Ts are subtype of Entity
class User<...Ts extends Entity> {}

By the end It would be great if we can specify a range or at least the max number of parameters.

// just to make clear the idea, maybe you can find a better syntax
class User<...Ts.(5,20)> {} // amount between 5 and 20
class User<...Ts.20> {} // max 20

Thanks!
I hope I was clear, and I hope you'll like the idea.

@elbeicktalat elbeicktalat added the feature Proposed language feature that solves one or more problems label Jun 6, 2022
@mateusfccp
Copy link
Contributor

Seems to be a duplicate of #1774?

@elbeicktalat
Copy link
Author

Seems to be a duplicate of #1774?

I don't think like so bro, it's similar only in the generic part, but my proposal is allowing spread operators as parameters of functions/methods.

@lrhn
Copy link
Member

lrhn commented Jun 6, 2022

More like duplicate of #1014 and #893.

@mateusfccp
Copy link
Contributor

mateusfccp commented Jun 7, 2022

Sorry, I only read the last part. So it seems a duplicate of many different issues. Should we close this one?

@elbeicktalat
Copy link
Author

elbeicktalat commented Jun 7, 2022

Sorry, I only read the last part. So it seems a duplicate or many different issues. Should we close this one?

Don't worry bro, I think we can close it to not make confusing. By the end it's team decision.
Best regards!

@lrhn lrhn closed this as completed Jun 7, 2022
@lrhn lrhn added the state-duplicate This issue or pull request already exists label Jun 7, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature Proposed language feature that solves one or more problems state-duplicate This issue or pull request already exists
Projects
None yet
Development

No branches or pull requests

3 participants