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

Add extensions on non-function typedefs #2284

Closed
sebastianbuechler opened this issue Jun 8, 2022 · 2 comments
Closed

Add extensions on non-function typedefs #2284

sebastianbuechler opened this issue Jun 8, 2022 · 2 comments

Comments

@sebastianbuechler
Copy link

sebastianbuechler commented Jun 8, 2022

In the original issue for introducing typedefs for non-function types, I saw questions about how extensions would work with this new feature. However, it seems that this did not make the cut yet.

The purpose of this could be something like this to parse more conveniently json lists of objects:

typedef Cars= List<Car>;

extension on Cars{
  Cars fromJson(List json) {
    final Cars cars = [];
    for (final car in json) {
      cars.add(Car.fromJson(car as Map<String, dynamic>));
    }

    return cars;
  }
}

Original comment:
That's how it works already with extensions on functions, so likely.

typedef VoidCallback = void Function();

extension on VoidCallback {
  void foo() {}
}

void main() {
  void Function() test = () {};

  test.foo();
}

Originally posted by @rrousselGit in #65 (comment)

@eernstg
Copy link
Member

eernstg commented Jun 8, 2022

The non-function type alias feature does work with extensions:

class Car {}

typedef Cars = List<Car>;

extension on Cars {
  void foo() {
    print('This is foo!');
  }
}

void main() {
  <Car>[].foo(); // Compiles, runs, and prints.
}

However, it looks like your example is intended to make fromJson a static method. You don't show any invocations of fromJson, so we can't tell, but it would in any case be a static method on the extension, so you would have to give the extension a named (say, E) and then you would be able to call it as E.fromJson(...).

If you wish to be able to declare static methods in an extension and call them as if they had been static methods on something (for instance, if the extension is on T where T is a class, they could be treated as static methods on T), then the right issue could be #723 (but there may be several similar ones).

@sebastianbuechler
Copy link
Author

The non-function type alias feature does work with extensions:

class Car {}

typedef Cars = List<Car>;

extension on Cars {
  void foo() {
    print('This is foo!');
  }
}

void main() {
  <Car>[].foo(); // Compiles, runs, and prints.
}

However, it looks like your example is intended to make fromJson a static method. You don't show any invocations of fromJson, so we can't tell, but it would in any case be a static method on the extension, so you would have to give the extension a named (say, E) and then you would be able to call it as E.fromJson(...).

If you wish to be able to declare static methods in an extension and call them as if they had been static methods on something (for instance, if the extension is on T where T is a class, they could be treated as static methods on T), then the right issue could be #723 (but there may be several similar ones).

You're right. I first did a static method and then tried to call Cars.fromJson(json). With the naming of the extension (i.e. CarsParser) I was able to achieve it as you proposed. Thank you very much!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants