Skip to content

Allow abstract variable declarations in interfaces. #798

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

Closed
lrhn opened this issue Jan 27, 2020 · 4 comments
Closed

Allow abstract variable declarations in interfaces. #798

lrhn opened this issue Jan 27, 2020 · 4 comments
Labels
request Requests to resolve a particular developer problem state-duplicate This issue or pull request already exists

Comments

@lrhn
Copy link
Member

lrhn commented Jan 27, 2020

Dart has no abstract variable declaration. If you need to add an instance field to an interface, without introducing a backing cell in the class, you have to write both a getter and a setter.

In the case of classes that are only used as interfaces, you currently can write a field declaration:

abstract class MyInterface {
  int age;
  String name;
}

As long as everybody implements this interface instead of extending it, it doesn't matter that the class technically isn't abstract and allocates memory for the two members in every instance. After all, there will be no instances.

When we move to Null Safety (nee NNBD), that interface will no longer work.
Both fields will become a non-nullable, and therefore they need to be initialized by the constructor.
The interface has no constructor, and currently doesn't need one. That means that it gets the default constructor which doesn't initialize any fields (could be fixed by #469, though).

So, to avoid the issue, the interface owner can either add a factory constructor like

factory MyInterface._() => throw UnsupportedError("Dammit Jim, I'm an interface, not a class!");

or change the declarations to getters and setters.
Either choice introduces otherwise unnecessary noise.

It would be nice if there was a way to declare a field abstract without too much extra syntax.

@lrhn lrhn added the request Requests to resolve a particular developer problem label Jan 27, 2020
@lrhn
Copy link
Member Author

lrhn commented Jan 27, 2020

@rrousselGit
Copy link

rrousselGit commented Jan 27, 2020

Nice!
Another use-case for that is that it works as syntax sugar for abstract properties on mixins:

mixin Example {
  int get a;
  set a(int value);

  abstract String b;
}

class Concrete with Example {
  @override
  int a;
  @override
  String b;
}

@lrhn
Copy link
Member Author

lrhn commented Jan 27, 2020

True, mixins is another use case where you might want to declare abstract fields. Good one.

@lrhn
Copy link
Member Author

lrhn commented Jan 27, 2020

Hmm, appears we already had #44, I'll just defer to that instead.

@lrhn lrhn closed this as completed Jan 27, 2020
@lrhn lrhn added the state-duplicate This issue or pull request already exists label Jan 27, 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

2 participants