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

Allow empty type parameters in type annotations and infer generics for initialized variable declarations (diamond operator) #10611

Closed
justinfagnani opened this issue May 12, 2013 · 5 comments
Labels
area-language New language issues should be filed at https://github.com/dart-lang/language closed-obsolete Closed as the reported issue is no longer relevant P3 A lower priority bug or feature request type-enhancement A request for a change that isn't a bug

Comments

@justinfagnani
Copy link
Contributor

justinfagnani commented May 12, 2013

A variable declaration with generics often includes visually redundant type information, for example:

Map<String, Symbol> _cache = new Map<String, Symbol>();

Removing the generics from either side does have effects though, especially in checked mode. We should support inferring generics for initialized variable declarations. This would allow type checks in checked-mode, but reduce visual redundancy:

Map<> _cache = new Map<String, Symbol>();
@gbracha
Copy link
Contributor

gbracha commented May 13, 2013

Just to clarify:

To reduce redundancy, one would focus on the left hand side. Simply writing

Map _cache = new Map<String, Symbol>();

impacts static checking. It has no effect on behavior in production mode. In checked mode, we get a check that the produced map has the type args String and Symbol. If we infer this, the checked mode test is pointless. So this is really about static checking. One would like the more precise type without the redundant typing.

The editor support local type inference which addresses this (I'm not saying that is a good thing). This is a proposal for doing that for parameterized types at the language level.


Added Accepted label.

@DartBot
Copy link

DartBot commented May 13, 2013

This comment was originally written by LouisSt...@gmail.com


Alternate approach as mentioned on G+ is https://code.google.com/p/dart/issues/detail?id=9401 (I like its simplicity, as it seems to behave more how I "expect" inference to work...)

@justinfagnani
Copy link
Contributor Author

Gilad,

If we infer the parameters, the checked-mode check for initialization is pointless, yes, but subsequent assignments and checks are worthwhile. Consider

Map<> cache = new Map<String, Symbol>();
// later on...
cache = makeNewCache();

We want the checked-mode check that makeNewCache really returns a Map<String, Symbol>, so just inferring in the Editor is not enough.

@justinfagnani
Copy link
Contributor Author

Issue #9401 is slightly different, as it refers to fixing the variable type in an initialized declaration even when using var.

@justinfagnani justinfagnani added Type-Enhancement P3 A lower priority bug or feature request area-language New language issues should be filed at https://github.com/dart-lang/language labels May 13, 2013
@kevmoo kevmoo added type-enhancement A request for a change that isn't a bug and removed accepted labels Feb 29, 2016
@matanlurey
Copy link
Contributor

Dart2 has type inference that allows the use of var or final here instead of Map<>.

@matanlurey matanlurey added the closed-obsolete Closed as the reported issue is no longer relevant label Jun 22, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-language New language issues should be filed at https://github.com/dart-lang/language closed-obsolete Closed as the reported issue is no longer relevant P3 A lower priority bug or feature request type-enhancement A request for a change that isn't a bug
Projects
None yet
Development

No branches or pull requests

5 participants