Skip to content

Lint: Avoid using function types as a runtime Type #57827

Open
@pq

Description

@pq

From @matanlurey on November 13, 2018 1:29

I don't know how this lint request will interact with the generalized typedef language proposals

In Dart 2.x, typedef relates directly to its structural type, and is not a "type" itself. Which means:

typedef A = void Function();
typedef B = void Function();
void main() {
  var set = Set();
  set.add(A);
  set.add(B);
  print(set.length); // 1
}

Due to the severe restrictions of using mirrors in any real code, treating a typedef definition as a runtime Type is not useful in anyway, and can be very confusing/error prone to users (see above). There is some resistance to amending the style guide (dart-lang/site-www#1221), which I think is on point - so instead I opened this request.

With this lint, you'd see:

typedef A = void Function();
typedef B = void Function();
void main() {
  var set = Set();
  // LINT:
  set.add(A);
  //            ^
  // LINT: AVOID using a typedef or function type as a runtime type literal.
  set.add(B);
  //            ^
  // LINT: AVOID using a typedef or function type as a runtime type literal.
  print(set.length); // 1
}

I don't see any real downsides to this lint.

/cc @natebosch @munificent @leafpetersen @srawlins

Copied from original issue: #35144

Activity

added
P2A bug or feature request we're likely to work on
on Nov 13, 2018
pq

pq commented on Nov 13, 2018

@pq
MemberAuthor

From @stereotype441 on November 13, 2018 23:44

@pq should this bug be moved into the linter repo?

added
type-enhancementA request for a change that isn't a bug
and removed
P2A bug or feature request we're likely to work on
on Nov 13, 2018
eernstg

eernstg commented on Nov 16, 2018

@eernstg
Member

Interesting! This seems to be related to another topic that we've discussed from time to time, namely branded types. Here's a proposal for some support in that direction, with some remarks near the end about why it is related to this issue: #57828.

added
P3A lower priority bug or feature request
on Oct 11, 2022
added
area-devexpFor issues related to the analysis server, IDE support, linter, `dart fix`, and diagnostic messages.
and removed on Feb 21, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    P3A lower priority bug or feature requestarea-devexpFor issues related to the analysis server, IDE support, linter, `dart fix`, and diagnostic messages.customer-google3devexp-linterIssues with the analyzer's support for the linter packagelinter-lint-requesttype-enhancementA request for a change that isn't a bug

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @pq@srawlins@devoncarew@bwilkerson@eernstg

        Issue actions

          Lint: Avoid using function types as a runtime `Type` · Issue #57827 · dart-lang/sdk