-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Description
This issue was originally filed by [email protected]
Since types in Dart are basically classes, I'd like to see the possibility to inherit from types. Two Suggestions:
Suggestion 1:
class Password extends String {
// Constructor must be const and must call super
Password(value) : super(value) {
if(length < 6) throw new PasswordToShortException();
//... more methods if necessary
}
}
var x = new Password("secret");
assert(x is String);
assert(x.length == 6);
assert(x == "secret");
Methods of String, num, etc. may not be overridden (ensures symmetry with primitives).
Suggestion 2:
typedef EvenPositive(value) extends int {
assert(isEven(value));
assert(isNegative(value) == false);
}
EvenPositive x = -4; // error in checked mode
The type's body may only call assertions and primitive type operations.