Skip to content
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

Enhancement: Allow extending num, String etc. #3888

Closed
DartBot opened this issue Jun 27, 2012 · 6 comments

Comments

@DartBot
Copy link

commented Jun 27, 2012

This issue was originally filed by webseiten.des...@gmail.com


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.

@iposva-google

This comment has been minimized.

Copy link
Contributor

commented Jun 28, 2012

Added Area-Language, Triaged labels.

@gbracha

This comment has been minimized.

Copy link
Contributor

commented Jun 29, 2012

a) Terminology note: The request makes a distinction between types and classes that has no meaning in Dart. I'm renaming the bug so that it's clear what it is about.

b) The main point is well taken: there are a small number of types in Dart that cannot be extended. It can certainly be attractive to subclass them.

You could almost think of int, double, bool, Null and String as final classes in Java (num doesn't fit that mold as it has subtypes). By design, we don't support final classes, to minimize complexity and avoid code where you cannot subclass (much as the submitter wants). Instead of final classes, we chose to have list of types that cannot be extended.

Why: because these are critical for efficiency. Its not a very purist argument, but pragmatically most people would trade the flexibility for a faster system.

We might be able to relax things in the future - especially String. Or we may decide never to do this and close the bug. Stay tuned.


Set owner to @gbracha.
Added this to the Later milestone.
Removed Type-Defect label.
Added Type-Enhancement, Accepted labels.
Changed the title to: "Enhancement: Allow extending num, String etc.".

@DartBot

This comment has been minimized.

Copy link
Author

commented Jan 25, 2014

This comment was originally written by ruihfaz...@gmail.com


"The request makes a distinction between types and classes that has no meaning in Dart."

that would be a good reason to allow derivation from builtin data types or "classes" as you prefer to call them, no?

i abandoned c++ because of this. since we cannot derive from lets say float a class must make use of an internal data member and therefor i had to redirect ALL operators to it. By allowing derivation from float all operator and functionalities would have been inherited.

@kasperl

This comment has been minimized.

Copy link
Contributor

commented Jul 10, 2014

Removed this from the Later milestone.
Added Oldschool-Milestone-Later label.

@kasperl

This comment has been minimized.

Copy link
Contributor

commented Aug 4, 2014

Removed Oldschool-Milestone-Later label.

@munificent

This comment has been minimized.

Copy link
Member

commented Dec 19, 2016

Because of Flutter, we care increasingly about compilation to native code. Part of that means having efficient support for unboxed value types. Being able to implement the primitive number types would interfere with that, so we don't intend to go in that direction.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
6 participants
You can’t perform that action at this time.