Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Impossible to define field of parameterized type with function type #511

Closed
rakudrama opened this issue Nov 18, 2011 · 4 comments
Closed
Assignees
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

@rakudrama
Copy link
Member

In the following example I would like to declare compareFn as having the type E x E -> int.
Dart offers no way to do this. An attempt to declare the type directly conflicts with the syntax of a local function definition. Local typedefs are not supported.

class Methods<E> {
  final compareFn; // This needs improving

  Methods(bool compare(E a, E b)) : compareFn = compare;

  bool lessThan(E a, E b) => compareFn(a, b) < 0;
}

main() {
  var m = new Methods<int>((int a, int b) => a.compareTo(b));

  print('1 < 2 = ${m.lessThan(1, 2)}');
  print('2 < 2 = ${m.lessThan(2, 2)}');
}

It seems that the tightest bound is a binary function returning int. The best of the poor alternatives is to have a top-level typedef for Dynamic x Dynamic -> int.

@DartBot
Copy link

DartBot commented Nov 18, 2011

This comment was originally written by drfibonacci@google.com


Added Triaged label.

@gbracha
Copy link
Contributor

gbracha commented Dec 15, 2011

Good point.

PS: Bugs against the language are really bugs against the spec. If the spec is wrong, that is a bug. If the language is not too your liking, no matter how displeased you are, it is a feature request.


Removed Type-Defect label.
Added Type-Enhancement label.

@anders-sandholm
Copy link
Contributor

Added this to the Later milestone.

@rakudrama
Copy link
Member Author

This enhancement is now implemented:

typedef bool CompareFunction<E>(E a, E b);

class Methods<E> {
  final CompareFunction<E> compareFn;


Added Fixed label.

@rakudrama rakudrama added Type-Enhancement area-language Dart language related items (some items might be better tracked at github.com/dart-lang/language). labels Mar 1, 2013
@rakudrama rakudrama added this to the Later milestone Mar 1, 2013
@kevmoo kevmoo added type-enhancement A request for a change that isn't a bug and removed type-enhancement labels Mar 1, 2016
nex3 pushed a commit that referenced this issue Aug 31, 2016
- Instead of using the raw runtime type that DDC uses for its type
  checks, use a WrappedType that correctly implements Type's interface.
- Compile class literals to wrap the type in a WrappedType.
- Make Object.runtimeType() do the same thing.

Fixes #488. Fixes #511.

R=jmesserly@google.com

Review URL: https://codereview.chromium.org/1944483002 .
copybara-service bot pushed a commit that referenced this issue Feb 8, 2023
dartdoc:
494a6bed  2023-02-02  Sam Rawlins  Bump markdown dep to a pre-release of 7.0.0 (#3321)
ad6f1612  2023-02-02  Devon Carew  introduce an environmentProvider abstraction (#3324)
d0b73000  2023-02-02  Sam Rawlins  Prepare testing code for some upcoming Warnings (#3322)
936a7888  2023-02-01  Sam Rawlins  Add text about front-end manual testing (#3286)
cbee812d  2023-02-01  Sam Rawlins  Avoid renaming generator params (#3316)
bcaa0349  2023-02-01  Sam Rawlins  Privatize Library.sdkLib and move getClassByName (#3317)
af32def5  2023-01-31  dependabot[bot]  Bump github/codeql-action from 2.1.39 to 2.2.1 (#3313)
528e97da  2023-01-31  dependabot[bot]  Bump actions/cache from 3.2.3 to 3.2.4 (#3312)
54020672  2023-02-01  Parker Lougheed  Begin changelog for next release after 6.1.5 (#3315)

markdown:
f51c24c  2023-02-01  Zhiguang Chen  Prepend a line ending if the p tag is removed and there is an element before it in a *tight* list (#513)
dd3e3a1  2023-01-31  dependabot[bot]  Bump dart-lang/setup-dart from 1.3 to 1.4 (#510)
5f25fc9  2023-01-31  dependabot[bot]  Bump actions/checkout from 3.2.0 to 3.3.0 (#511)
54266b2  2023-01-31  Kevin Moore  Prepare to release v7.0.0 (#509)
d6ceff9  2023-01-31  Zhiguang Chen  Add line endings before HTML blocks (#508)
ccd3c8b  2023-01-29  Zhiguang Chen  Rewrite link reference definitions (#506)
e8f84dc  2023-01-24  Kevin Moore  Update GFM to 0.29.0.gfm.7 (#507)
93f67b0  2023-01-20  Zhiguang Chen  Add an `enableTagfilter` option to `HtmlRenderer` to eanble GFM `tagfilter` extension (#447)
d110770  2023-01-13  Zhiguang Chen  Rewrite SetextHeaderSyntax (#500)
c1b9bc6  2023-01-09  Sam Rawlins  Migrate from no-implicit-casts to strict-casts (#504)
3e78c08  2023-01-01  dependabot[bot]  Bump actions/checkout from 3.0.2 to 3.2.0 (#501)
417747f  2022-12-15  Zhiguang Chen  Refactor list syntax (#499)
30ec05f  2022-11-29  Zhiguang Chen  Do not escape single quote(apostrophe) inside code (#498)
c3d1136  2022-11-29  Zhiguang Chen  introduce a Line class (#494)
Change-Id: I9d971b927b72500ab84cce461a60fe1a86818e2e
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/281543
Reviewed-by: Samuel Rawlins <srawlins@google.com>
Commit-Queue: Devon Carew <devoncarew@google.com>
@pq pq mentioned this issue Nov 18, 2024
11 tasks
This issue was closed.
# for free to join this conversation on GitHub. Already have an account? # 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

5 participants