Skip to content

Enhancement: Allow extending num, String etc. #3888

@DartBot

Description

@DartBot

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.

Metadata

Metadata

Assignees

Labels

P2A bug or feature request we're likely to work onarea-languageDart language related items (some items might be better tracked at github.com/dart-lang/language).closed-not-plannedClosed as we don't intend to take action on the reported issuetype-enhancementA request for a change that isn't a bug

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions