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
Allow interface as its own construct (not just class modifier) #3736
Comments
I'm not sure that constructable interfaces, that is, interfaces that even importing libraries can construct, carry their weight. I think they should be limited such that only the local library can construct them. That's because I think you can sidestep the 'cannot extend' restriction by constructing an instance, then wrapping it in a class that implements its interface, which effectively allows you to inherit its implementation: // foo.dart
interface class Foo {
m(){}
} // bar.dart
import 'foo.dart';
class Bar implements Foo {
final foo = Foo();
m() => foo.m(); // Sort of like extending; just more work!
} However, that isn't quite the same as extending, because you don't inherit, for free, the private implementation/members present in the superclass. But if there's a private interface in the superclass, then why give the superclass the TLDR: I think non-abstract interfaces should have no public constructors. I might be way off on that, but I can't think of a good reason for wanting an interface that an outside library can construct. Are there any? If not, that raises another question: should there be a warning or lint against non-abstract
I can kind of see where that is coming from. It is definitely weird; especially point 2. But I'm not sure that we should have more than one way to do the same thing. I think these are my issues:
However, I think there is value in allowing an interface to be constructed and extended from within the same library. And, more importantly, I think there is value in all interfaces being potentially extendable from within the same library. Right now, an author can add an implementation to their I have a proposal that may kill two birds with one stone:
The idea is that an interface's constructors would be explictly/implicitly private. It also hinges on the premise that public constructors on interfaces are bad. My proposal removes |
None of that is allowed in |
I'd be fine with allowing I don't see any advantage in adding more restrictions than what Having interfaces with no implementation is not a goal. It's not "using interfaces as intended", because there isn't a single intent for what you can use interfaces for. There is no problem with having implementation, if our actually used. |
Well it would at least solve one of my complaints with the existing implementation (too wordy and hard to remember), but I would prefer if it went further than that.
Why have any compile errors? The programmer could simply choose not to do any of the things the compiler would have prevented them from doing. :)
Yes but that is a common scenario, I don't have the data to prove it, but I suspect in a many cases, maybe even most cases, an Take the flutter repo for example, I found 7 different libraries where
Surely individual programmers have an intent for what they wish to use their interfaces for, and I suspect a pure interface is the most common intent.
The analyzer does not do that. Nor are there any lint rules about using an interface in an impure way. The only relevant analyzer warning I am aware of is when you reopen an interface.
|
In dart there is support already for creating interfaces using a class modifier, but the existing support is weird and unintuitive.
Consider this example:
For starters, it is a bit of a keyword soup to remember
abstract interface class
whenever you want aninterface
, and you almost never would want justinterface class
.It is also weird that
I propose allowing a separate interface construct that would not allow any of the above.
The errors should guide the user towards using
interface
as intended:The text was updated successfully, but these errors were encountered: