-
Notifications
You must be signed in to change notification settings - Fork 205
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
Record spreading #2128
Comments
Me too!
Assuming we stick with the idea to use
YES. [jack nicholson nodding.gif] Also, we should allow |
I really do not want the If you want to represent your class as a tuple, I'd just have a method or getter returning that tuple. That makes it explicit, and documented by name, which semantic view of the object you're using. I'm not sold on The places where it makes sense to use |
I'm not particularly attached to them, but they do seem to solve the problem that I want to solve which is giving classes a way to opt into and control their positional destructuring.
I think implementing Implementing
Good point.
Yes, rest params was where I initially thought to support |
Have you also considered introducing support to spread record typedefs in parameter lists as well? Example of how this can greatly improve passing parameters to extended/nested widgets: typedef CommonButton = ({
VoidCallback? onPressed,
VoidCallback? onLongPress,
ValueChanged<bool>? onHover,
ValueChanged<bool>? onFocusChange,
ButtonStyle? style,
FocusNode? focusNode,
bool autofocus = false,
required Widget child,
});
class OutlinedButton extends ButtonStyleButton {
// Only for OutlinedButton!
final bool someSpecializedThing;
OutlineButton(CommonButton... commonButton, {this.someSpecializedThing}) : super(...commonButton);
}
// Not exactly a clean example, but shows what could be possible:
class MyParticleButton extends StatelessWidget {
final CommonButton commonButton;
// Also works for function types
final Widget Function(CommonButton...) builder;
final Color particleColor;
MyParticleButton(CommonButton... commonButton, {Key? key, this.builder = OutlinedButton.new, this.particleColor}) : super(key: key);
Widget build(BuildContext context) {
return ParticlePainter(
child: builder(
...commonButton,
// Like map spreads, allows overwriting positional parameters?
onPressed: ParticlePainter.wrap(context, commonButton.onPressed),
),
);
}
}
// Usage:
MyParticleButton(child: Text('Tap me!'))
MyParticleButton(builder: ElevatedButton.new, child: Text('I am filled with color (and particles)!')) |
Shouldn't it be possible to spread records even if they're represented by a dynamic type? |
Whether we can allow dynamic record operations depends on how we want to be able to implement records. There are implementation strategies (that I favor) which requires all record types occurring in the program to be known at compile-time. So, that's why I don't want to allow you to do any operation on an untyped record, other than checking that it has a specific known structure. |
Can records be created during runtime? void handleWebSocket(WebSocket webSocket) {
webSocket
.map((string) => decodeJson(string))
.listen((message) {
var echo = message.echo;
print("Message to be echoed: $echo");
var response = encodeJson((response: echo));
webSocket.add(response);
}, onError: (error) {
print('Bad WebSocket request');
});
} The spread operator with dynamic types would be more useful if so. It would allow the creation of something more equivalent to a JSON in Dart than a Map, but I'm not sure if it's of interest of this proposal or even the Dart language. I guess it would be useful especially when interacting with APIs. |
I'm moving this over to patterns-later. I definitely want it, but I think it would be really hard to get this designed and into the initial release. |
The current records proposal does not introduce a "spreading" operator.
I think it should have one.
A "record spread", with syntax
... recordExpression
, would be allowed inside any record expression or argument list.The
recordExpression
must have a static type which is an actual record type (notdynamic
and notRecord
orT extends Record
).It works by, effectively, inlining every member of the tuple at the spread-point. So,
would inline the elements of
point
as elements of the new record at the point of the spread.It would also work in argument lists.
This is a static operation which requires knowing the type of the spreadee and allows knowing the structure of the result.
It won't allow something like
because neither
a
norb
have known structures.That also means that it can be desugared into, e.g,
(point[0], point[1], color: Color.red)
if we have a syntax for directly accessing members of a record. (If we don't, the desugaring would include a pattern match of some sort, and might not be directly expressible as an expression in the language.)The text was updated successfully, but these errors were encountered: