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

Static extension methods defined in an extension on Enum do not get added to ClassMirror.staticMembers. #1037

Closed
akasher opened this issue Jun 18, 2020 · 3 comments
Labels
request Requests to resolve a particular developer problem state-duplicate This issue or pull request already exists

Comments

@akasher
Copy link

akasher commented Jun 18, 2020

enum Roles{
owner,
admin,
user}

extension RolesParser on Roles{
static Roles parse(String value) {
    final Roles role = tryParse(value);
    if (role != null)
      return role;
  }
  static Roles tryParse(String input) {
    final String value = input.trim();
    return Roles.values.firstWhere((element) => element.toString().split('.')[1].toUpperCase() == value.toUpperCase());
  }
}
@akasher akasher added the request Requests to resolve a particular developer problem label Jun 18, 2020
@leafpetersen
Copy link
Member

If I understand the request correctly, you are asking to be able to introspect on the set of extensions which extend a given enum using the ClassMirror for the enum? I think this is probably working as intended. Extensions aren't really attached to the class they extend, and it feels surprising to me that someone in some completely unrelated library adding an extension would change the entries of a ClassMirror.

But @rmacnak-google would probably be the right person to comment here.

@lrhn
Copy link
Member

lrhn commented Jun 22, 2020

There are two issues here.

First of all, the attempt to add static members to a class using static extensions. That simply does not work in the current Dart. There is no difference between the extension written here and a class RolesParser containing the same static methods. They can still only be called using RolesParser.tryParse/RolesParser.parse, not as Roles.tryParse or Roles.parse.

Second, extensions do not show up in reflection on the target class. There is nothing inherently preventing that, but I do not believe that there is currently any easy way to access extension methods using mirrors. Extension methods are desugared into top-level functions, in this case named RolesParser.tryParse and RolesParser.parse (which are otherwise not valid declaration names), and you can find them using dart:mirrors using LibraryMirror.declarations (and you probably shouldn't be able to find them that way).

The former issue is a language issue, the latter is a library issue and belongs in the SDK repository.

@lrhn
Copy link
Member

lrhn commented Aug 18, 2020

Closing this for #723 and dart-lang/sdk#43086.

@lrhn lrhn closed this as completed Aug 18, 2020
@lrhn lrhn added the state-duplicate This issue or pull request already exists label Aug 18, 2020
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 state-duplicate This issue or pull request already exists
Projects
None yet
Development

No branches or pull requests

3 participants