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

Document F-bounds and inference using bounds for 3.7 #6403

Merged
merged 17 commits into from
Feb 18, 2025

Conversation

MaryaBelanger
Copy link
Contributor

@MaryaBelanger MaryaBelanger commented Feb 10, 2025

Fixes #6258

I added the section "Self-referential type parameter restriction" to the generics page, to support the "Inference using bounds" section on the Type system page (staged links).

This is a difficult topic to summarize simply enough for the language tour! I did my best, but please let me know if anything needs to change / if this misses the point of the new feature.

@dart-github-bot
Copy link
Collaborator

dart-github-bot commented Feb 10, 2025

Visit the preview URL for this PR (updated for commit a180b33):

https://dart-dev--pr6403-inference-bounds-m64rfkos.web.app

@MaryaBelanger
Copy link
Contributor Author

MaryaBelanger commented Feb 10, 2025

@parlough The error from test / analyze is, I think, just because inference using bounds is not stable yet...?

error - examples/type_system/lib/strong_analysis.dart:171:3 - Couldn't infer type parameter 'X'.
          
          Tried to infer 'C' for 'X' which doesn't work:
            Type parameter 'X' is declared to extend 'A<X>' producing 'A<C>'.
          The type 'C' was inferred from:
            Parameter 'x' declared as     'X'
                          but argument is 'C'.
          
          Consider passing explicit type argument(s) to the generic.
          
           - could_not_infer
   info - examples/misc/test/language_tour/generics_test.dart:70:[16](https://github.com/dart-lang/site-www/actions/runs/13251876512/job/36991328626?pr=6403#step:6:17) - Unnecessary use of an abstract class. Try making 'compareTo' a top-level function and removing the class. - one_member_abstracts

For the info message though, do you have an idea for a better way to write that? That's how I could get it to work in dartpad on main, but lmk if there's something neater I could do. Thanks!

@parlough
Copy link
Member

@parlough The error from test / analyze is, I think, just because inference using bounds is not stable yet...?

Yep! Now I've updated main to 3.7 and merged it in, analysis seems to be working now 👍

For the info message though, do you have an idea for a better way to write that?

No, I think that's good. I pushed a change that adds an ignore comment. I also added the interface class modifier, which I think might be a good signal to not trigger the lint. I'll explore making that change in the SDK.

Copy link

@chloestefantsova chloestefantsova left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The edits LGTM. Thank you so much, Marya! I'd like to known @eernstg's opinion on this too.

@MaryaBelanger
Copy link
Contributor Author

Thank you Chloe! And yes I will definitely wait for Erik's input, looking forward to it!

@parlough parlough mentioned this pull request Feb 14, 2025
Copy link
Member

@eernstg eernstg left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the kind words! I added a bunch of comments, hope they are helpful!


void main() {
f(B()); // OK.
f(C()); // Inference fails, compile-time error.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Inference fails when this feature isn't available. When the feature is available, inference succeeds. I don't think that's obvious at this point. I also don't know how it could be explained in that comment without breaking the 80-or-whatever limit on the line length. ;-)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah I can see from all your suggestions that I wrote this section kind of on the line between treating this like a new feature/improvement, and trying to make it sound like this is "just the way it works". I'll clean up the whole section with that in mind, thank you for the thorough review!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe someone can help with code comment best practices, but I added a longer explanation here:

  f(C()); // OK. Without using bounds, inference relying on best-effort
  // approximations would fail after detecting that C is not a subtype of A<C>.

Copy link
Member

@eernstg eernstg left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added a couple of comments.


With inference using bounds, Dart can *deconstruct* type arguments,
extracting type information from a generic type parameter's bound.
This allows functions like `f1` in the following example to preserve both the
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When we don't have f2, perhaps a name like f would suffice. (Unless there's a name clash with some other declaration named f nearby, but I couldn't see any).

@MaryaBelanger MaryaBelanger merged commit 01664e6 into main Feb 18, 2025
10 checks passed
@MaryaBelanger MaryaBelanger deleted the inference-bounds branch February 18, 2025 17:42
Copy link
Member

@parlough parlough left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @MaryaBelanger and everyone for working so hard on figuring out what to include here and how to word it.

Looks good to me! Just one suggestion to consider:

@MaryaBelanger MaryaBelanger restored the inference-bounds branch February 18, 2025 17:44
@MaryaBelanger
Copy link
Contributor Author

Ahh sorry I merged it too fast! I'll reopen a pr for your fixes, thank you for checking

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[3.7] Document language feature inference-using-bounds
5 participants