Skip to content
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

Improve typedef Usability for Named Tuple-like Structures in Dart #59607

Closed
stephane-archer opened this issue Nov 25, 2024 · 3 comments
Closed
Labels
area-language Dart language related items (some items might be better tracked at github.com/dart-lang/language). type-enhancement A request for a change that isn't a bug

Comments

@stephane-archer
Copy link

When using typedef for defining tuple-like structures, the behavior feels unintuitive and error-prone. For example, in the code below:

import 'package:flutter/material.dart';

typedef FeedbackData = (String message, Color color);

void Function(FeedbackData) getDisplayFeedbackForTheUser(
    BuildContext context) {
  return (feedbackData) {
    final snackBar = SnackBar(
      backgroundColor: feedbackData.$2,
      content: Text(feedbackData.$1),
      duration: feedbackData.$2 != Colors.green ? Duration(seconds: 6) : Duration(seconds: 4),
    );
    ScaffoldMessenger.of(context).showSnackBar(snackBar);
  };
}

The FeedbackData fields (message and color) must be accessed using . $1 and . $2. This feels strange, non-descriptive, and introduces potential errors due to lack of clarity.

Current Workaround:

I can rewrite the typedef like this:

typedef FeedbackData = ({String message, Color color});
This makes field access more intuitive:

final snackBar = SnackBar(
  backgroundColor: feedbackData.color,
  content: Text(feedbackData.message),
  duration: feedbackData.color != Colors.green ? Duration(seconds: 6) : Duration(seconds: 4),
);

However, this approach makes creation tedious because I need to explicitly name every parameter when creating a FeedbackData object.

Suggestion:

Can the following behavior be supported for typedef?

typedef FeedbackData = (String message, Color color);

void Function(FeedbackData) getDisplayFeedbackForTheUser(
    BuildContext context) {
  return (feedbackData) {
    final snackBar = SnackBar(
      backgroundColor: feedbackData.color,
      content: Text(feedbackData.message),
      duration: feedbackData.color != Colors.green ? Duration(seconds: 6) : Duration(seconds: 4),
    );
    ScaffoldMessenger.of(context).showSnackBar(snackBar);
  };
}

Here, FeedbackData would behave like a named tuple, allowing both intuitive field access (feedbackData.message and feedbackData.color) and concise creation syntax.

Why This Matters:

The current behavior of typedef makes tuple-like constructs harder to use in a way that is both expressive and practical. Enhancing typedef usability for named field access would make writing cleaner, safer, and more readable code easier.

@dart-github-bot
Copy link
Collaborator

Summary: Current typedef handling of tuple-like structures is unintuitive and error-prone, requiring awkward $1, $2 access. Proposed improvement: allow named field access for cleaner, safer code.

@dart-github-bot dart-github-bot added area-language Dart language related items (some items might be better tracked at github.com/dart-lang/language). triage-automation See https://github.com/dart-lang/ecosystem/tree/main/pkgs/sdk_triage_bot. type-enhancement A request for a change that isn't a bug labels Nov 25, 2024
@mraleph
Copy link
Member

mraleph commented Nov 26, 2024

Duplicate of dart-lang/language#3487

@mraleph mraleph marked this as a duplicate of dart-lang/language#3487 Nov 26, 2024
@mraleph mraleph closed this as completed Nov 26, 2024
@mraleph mraleph removed the triage-automation See https://github.com/dart-lang/ecosystem/tree/main/pkgs/sdk_triage_bot. label Nov 26, 2024
@stephane-archer
Copy link
Author

While related, this concern a typedef and it's usage

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-language Dart language related items (some items might be better tracked at github.com/dart-lang/language). type-enhancement A request for a change that isn't a bug
Projects
None yet
Development

No branches or pull requests

3 participants