-
Notifications
You must be signed in to change notification settings - Fork 214
Parameter Groups #2245
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
Comments
Interesting! I think, though, that this might already be covered by declaring the parameter group as a record, and using a spread operator. In that sense it could be considered as a vote in favor of having that spread operator. Here's how that would look: // A constant record, using named fields corresponding to certain named parameters.
const minStart = (
mainAxisSize: MainAxisSize.min,
crossAxisAlignment: CrossAxisAlignment.start,
mainAxisAlignment: MainAxisAlignment.start,
);
// Example invocations where that record is 'spread' out, thus providing some actual arguments.
Column(...minStart, children: []);
Row(...minStart, children: []); |
For some simpler cases, we can tear-off the constructors: class MyWidget extends StatelessWidget {
@override
Widget build(BuildContext context) {
final widget = condition ? Column.new : Row.new;
return widget(
mainAxisSize: MainAxisSize.min,
crossAxisAlignment: CrossAxisAlignment.start,
mainAxisAlignment: MainAxisAlignment.start,
children: const [
Text('Item 1'),
Text('Item 2'),
Text('Item 3'),
],
);
}
} But it would be way nicer to have parameters-records equivalence. |
That’s good, I wasn’t aware of it.
|
The record proposal does not mention the spread operator (which would allow records to be passed in a way that desugars to several separate actual arguments), but it has been mentioned in a lot of discussions. It would be good if they were specified more precisely, but the main idea is as follows: A spread operator on a record This would (presumably) be a purely static mechanism, which means that it would only work when the static type of the record is the given record type (not |
Now I see. Thanks! Of course, count this as a vote on the record spread operator. |
Using records in formal and actual parameter listsApplies to all elements with parameter list: constructors, methods, functions, extensions… Getters//Returns the parameters as a record type
Element.paramsType
//Returns the formal parameters as a record expression
Element.paramsType record = element.paramsFormal;
//Returns the actual parameters as a record expression
Element.paramsType record = element.paramsActual; Spread (…)The … operator expands the record fields into the containing list:
If the expanded fields overlap with list items conflict resolution is a question:
ConstructorIt would be great if IDE wizards and autofill could support typed records. Although the record specification excludes the naming of record types, something similar can work: var flexParamRecord = Flex.paramsType(
mainAxisAlignment: MainAxisAlignment.start,
mainAxisSize: MainAxisSize.min,
) |
Problem
Handling long parameter lists either at declaration or at call/instantiation may require a lot of repeating and hard to read code. Flutter widgets are good examples where the actual parameters often just replace the default values for each instantiation:
Simplifying this can be done by
• adding specific named constructors with proper defaults, but that's an endless effort
• creating subclasses just to override defaults, but it seems to be an overkill resulting in non-shallow inheritance chains.
Proposal
As a new solution this proposal aims to create a new language feature called Parameter Group to make parameter lists shorter, easier to read and more reusable. For example, minStart is a Parameter Group:
A Parameter Group is a list of parameters including type, name and (default) value.
Parameter Groups are used inside parameter lists via the … (spread) operator, which expands all members of the group into the list.
Expansion is done virtually for the IDE wizards, autofill and documentation.
Parameter Groups can be
Parameter groups can be placed into
Example: because all Column constructor parameters are just passed to its superclass Flex we can simplify the Column() declaration using the implicit parameter group of the Flex constructor:
Advantages
General:
In declarations:
In calls:
Considerations
Related topics
The text was updated successfully, but these errors were encountered: