Allow abstract variable declarations in interfaces. #798
Labels
request
Requests to resolve a particular developer problem
state-duplicate
This issue or pull request already exists
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:
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
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.
The text was updated successfully, but these errors were encountered: