You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Dart allows you to to use interfaces in an impure way (especially within the same library it is defined). This lint would detect and warn against any of the following in an interface:
Non-abstract declarations
a. Non-abstract interfaces
b. Non-abstract properties
c. Non-abstract methods
Generative constructors (factory constructors are okay)
An interface using extends or with
Another class using extends on an interface
Kind
Style advice
Bad Examples
interfaceclassVehicle { // 1a. Non-abstract interfacefinalString make; // 1b. Non-abstract propertyfinalString model; // 1b. Non-abstract propertyVehicle(this.make, this.model); // 2. Generative constructorvoidmoveForward() { // 1c. Non-abstract methodprint('$make $model is moving forward!');
}
}
// 3. An interface using extends or withabstractinterfaceclassCarextendsVehicle {
Car(super.make, super.model);
}
// 4. Another class using extends on an interfaceclassHondaCivicextendsCar {
HondaCivic() :super('Honda', 'Civic');
}
Good Examples
abstractinterfaceclassVehicle {
abstractfinalString make;
abstractfinalString model;
voidmoveForward();
}
abstractinterfaceclassCarimplementsVehicle {}
classHondaCivicimplementsCar {
@overridefinalString make ='Honda';
@overridefinalString model ='Civic';
@overridevoidmoveForward() {
print('$make $model is moving forward!');
}
}
it's not a lint I'd want in the recommended set. That is, it's a style lint that one can opt in to, but not an official style recommendation.
About the individual points of warning:
class fields in an interface
Should be "concrete instance field declaration". A declaration of
abstractfinalint foo;
It's an instance variable declaration, but it doesn't introduce any implementation.
constructors in an interface
Should probably only be generative constructors. Having an interface with factory constructors creating instances of classes implementing the interface, is a common pattern.
method bodies in an interface
Can be combined with first item into:
"Non-abstract instance member declarations".
a class extending an interface (unless that class is also an interface)
Can only happen inside the same library.
I'd remove the exception. Other libraries cannot extend an interface declared class, which means that extending is not considered using it as an interface. This lint is about enforcing that the interface is only used as an interface.
So no exception.
an interface class that is not also abstract
Should have no effect if there is no generative constructor, and required if there is any abstract instance member (what must be abstract, so any instance member at all.)
But does prevent invoking a default constructor.
So sure.
creating an instance of an interface
Redundant with not allowing generative constructors and being abstract.
Might also want:
must not extend another class. (No extends or with clause.)
Otherwise the interface can inherit concrete instance members.
Description
Warn when using interfaces in an impure way.
Details
Dart allows you to to use interfaces in an impure way (especially within the same library it is defined). This lint would detect and warn against any of the following in an interface:
a. Non-abstract interfaces
b. Non-abstract properties
c. Non-abstract methods
extends
orwith
extends
on an interfaceKind
Style advice
Bad Examples
Good Examples
Discussion
See also dart-lang/language#3736 and dart-lang/sdk#55641.
The text was updated successfully, but these errors were encountered: