Skip to content

Add interfaces support in addition to abstract classes #1707

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

Open
LeftTwixWand opened this issue Jun 25, 2021 · 3 comments
Open

Add interfaces support in addition to abstract classes #1707

LeftTwixWand opened this issue Jun 25, 2021 · 3 comments
Labels
feature Proposed language feature that solves one or more problems

Comments

@LeftTwixWand
Copy link

Hi. For the last few years I've been working with C# and now using Dart and Flutter in personal and job projects.
And I feel the need in the interfaces as the aspect of Dart language.
I understand, that abstract classes provides the similar ways to get the guaranties about the interaction interface in the inherited children. But the interfaces will provides more strict understanding, about some entity behavior, and we could be sure, that our child won't inherit some useless behavior from the abstract parent.

@LeftTwixWand LeftTwixWand added the feature Proposed language feature that solves one or more problems label Jun 25, 2021
@Levi-Lesches
Copy link

But the interfaces will provides more strict understanding, about some entity behavior, and we could be sure, that our child won't inherit some useless behavior from the abstract parent.

Are you saying you want a way to declare an interface that has no implementation so that the subclasses have to implement everything? You can acheive this by using implements instead of extends.

class A { 
  void function() => print("This is bad");
}

class B extends A { }  // inherits the bad function

class B implements A {  // doesn't inherit anything. It's an error not to implement the function
  @override
  void function() => print("This is good");
}

void main() {
  A badClass = B();
  A goodClass = C();

  B.function();  // bad
  C.function();  // good
}

That being said, the whole OOP design is currently being revisited, you may want to check out #1696.

@lrhn
Copy link
Member

lrhn commented Jun 26, 2021

If you create an abstract class with a private constructor, say C._noInstances() { throw UnsupportedError("No instances"); }, to inhibit adding the default constructor, then the class can only be used as an interface.

@LeftTwixWand
Copy link
Author

@lrhn and @Levi-Lesches, thank You for these samples. But I'm still thinking, that to have the interface as the language aspect will be much easier, than to do some additional manipulations. If the interfaces by default could be only implemented - it will help to build more clear project architecture. And as I see - it's not so difficult to implement, because the Dart has already working with such functional in abstract classes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature Proposed language feature that solves one or more problems
Projects
None yet
Development

No branches or pull requests

3 participants