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

Expand workarounds on 'Fixing type promotion failures' page #5461

Open
parlough opened this issue Jan 12, 2024 · 2 comments
Open

Expand workarounds on 'Fixing type promotion failures' page #5461

parlough opened this issue Jan 12, 2024 · 2 comments
Labels
a.effective-dart Relates to the best practices explained in Effective Dart a.language Relates to the Dart language tour dev.type-promotion Relates to type promotion concepts and examples dev.type-system Relates to the type system in Dart e2-days Can complete in < 5 days of normal, not dedicated, work p3-low Valid but not urgent concern. Resolve when possible. Encourage upvote to surface. st.triage.ltw Indicates Lead Tech Writer has triaged

Comments

@parlough
Copy link
Member

parlough commented Jan 12, 2024

Page URL

https://dart.dev/tools/non-promotion-reasons.html

Page source

https://github.com/dart-lang/site-www/tree/main/src/tools/non-promotion-reasons.md

Describe the problem

We mostly present assigning fields to a shadowed local on this page, but I think an if-case statement is better in most cases if it matches your desired structure.

Expected fix

No response

Additional context

https://dart.dev/effective-dart/usage#consider-assigning-a-nullable-field-to-a-local-variable-to-enable-type-promotion should likely be updated as well.

@parlough parlough added a.effective-dart Relates to the best practices explained in Effective Dart a.language Relates to the Dart language tour p3-low Valid but not urgent concern. Resolve when possible. Encourage upvote to surface. e2-days Can complete in < 5 days of normal, not dedicated, work dev.type-system Relates to the type system in Dart dev.type-promotion Relates to type promotion concepts and examples labels Jan 12, 2024
@parlough
Copy link
Member Author

parlough commented Jan 17, 2024

Heya @munificent and @stereotype441 👋! In our documentation we often suggest assigning a field to a local variable to overcome type promotion failures.

With Dart 3, do you think it's fine if we expand this to suggest using if-case with a type check or non-null check for some situations? If you want or need to exit early, this won't fit all those situations currently (without dart-lang/language#2537), but in my experience this style covers most cases where promotion is needed.

So previously we'd suggest something like this:

class C {
  String? output;
  void f() {
    final output = this.output;
    if (output != null) {
      someFunctionThatExpectsNonNullString(output);
    } else {
      print('No output.');
    }
  }
}

Now we can also suggest (or even prefer):

class C {
  String? output;
  void f() {
    if (output case final output?) {
      someFunctionThatExpectsNonNullString(output);
    } else {
      print('No output.');
    }
  }
}

A related best practice question: If we do suggest a style like this, should we suggest matching the field name or suggest providing a new name for the resulting variable that relates to its promoted/tightened state?

Thanks for your thoughts! :D

@munificent
Copy link
Member

With Dart 3, do you think it's fine if we expand this to suggest using if-case with a type check or non-null check for some situations?

Yes! A thousand times, yes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
a.effective-dart Relates to the best practices explained in Effective Dart a.language Relates to the Dart language tour dev.type-promotion Relates to type promotion concepts and examples dev.type-system Relates to the type system in Dart e2-days Can complete in < 5 days of normal, not dedicated, work p3-low Valid but not urgent concern. Resolve when possible. Encourage upvote to surface. st.triage.ltw Indicates Lead Tech Writer has triaged
Projects
None yet
Development

No branches or pull requests

3 participants