Skip to content

Inference Enhancements - Generic Class Functions #888

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

Closed
D10100111001 opened this issue Mar 19, 2020 · 5 comments
Closed

Inference Enhancements - Generic Class Functions #888

D10100111001 opened this issue Mar 19, 2020 · 5 comments
Labels
request Requests to resolve a particular developer problem

Comments

@D10100111001
Copy link

D10100111001 commented Mar 19, 2020

It is really annoying to work with some limitations on the inference related to the dart language. Coming from C# and TypeScript and other typed languages it has always been easy to get good inference. This is related to #731.

Here is an example:

import 'package:flutter/material.dart';

class TreeView<T> extends StatelessWidget {
  const TreeView(
      {Key key,
      @required this.items,
      @required this.childFn,
      this.childSelector})
      : super(key: key);

  final List<T> items;
  final Widget Function(T item) childFn;
  final List<T> Function(T item) childSelector;

  @override
  Widget build(BuildContext context) {
    return SingleChildScrollView(
      child: Column(
        children: items.expand((item) => _buildItem(item)).toList(),
      ),
    );
  }

  List<Widget> _buildItem(T item) {
    return [
      childFn(item),
      if (childSelector != null)
        ...childSelector(item)
            .expand((childItem) => _buildItem(childItem))
            .toList()
    ];
  }
}

image

(new) TreeView TreeView({Key key, List items, Widget Function(AppComponent) childFn, List Function(AppComponent) childSelector})

The IDE is able to infer and show the correct tooltip regarding the component definition but the dart analyzer does not agree. If it isn't clear, essentially the analyzer is unable to infer to the type and it falls back to dynamic and I have implicit dynamic disabled.

image

Missing parameter type for 'item'.
Try adding an explicit type like 'dynamic', or enable implicit-dynamic in your analysis options file.dart(implicit_dynamic_parameter)

Is there a timeline on when this will be implemented? I see the referenced issue was created in 2016.

@D10100111001 D10100111001 added the request Requests to resolve a particular developer problem label Mar 19, 2020
@leafpetersen
Copy link
Member

Sorry, can you elaborate a bit? There are certainly known limitations to the inference we currently do, including the one you reference, but I don't see anything in the code that you included above that we shouldn't be able to infer, and if I test it out I haven't been able to find any dynamic code in it (I may be missing something). The IDE snippets you included seem to refer to different code? There is something odd in the error message you listed:

(new) TreeView TreeView({Key key, List items, Widget Function(AppComponent) childFn, List Function(AppComponent) childSelector})

That suggests that childSelector is returning List<dynamic> which is not the case in the snippet you sent: in that code, it is returning List<T>.

Can you verify that the issue you are seeing does in fact occur in your reproduction, and point out exactly where in the code you're seeing dynamic? Also, can you include the sdk version that you're seeing this in?

@D10100111001
Copy link
Author

@leafpetersen I have created a minimal reproducible example (MRE): https://dartpad.dartlang.org/c8b6d45927b383810bfeef7151416ca0

@leafpetersen
Copy link
Member

Thanks, yes your MRE is exactly an instance of #731 . I do hope to address this in a future language update, but since it is breaking to change inference (even if to improve it) it's not a trivial thing to do. I don't have an ETA on when this will bubble to the top of the queue, sorry!

@mraleph
Copy link
Member

mraleph commented Mar 20, 2020

@leafpetersen should this just marked as duplicate of #731?

@leafpetersen
Copy link
Member

@mraleph I believe so, yes. @D10100111001 if there are any issues here not covered by #731 feel free to re-open.

Duplicate of #731

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
request Requests to resolve a particular developer problem
Projects
None yet
Development

No branches or pull requests

3 participants