Navigation Menu

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 · 7 comments
Closed

Enhancement: Allow extending num, String etc. #3888

DartBot opened this issue Jun 27, 2012 · 7 comments
Assignees
Labels
area-language Dart language related items (some items might be better tracked at github.com/dart-lang/language). closed-not-planned Closed as we don't intend to take action on the reported issue P2 A bug or feature request we're likely to work on type-enhancement A request for a change that isn't a bug

Comments

@DartBot
Copy link

DartBot 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
Copy link
Contributor

Added Area-Language, Triaged labels.

@gbracha
Copy link
Contributor

gbracha 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
Copy link
Author

DartBot 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
Copy link

kasperl commented Jul 10, 2014

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

@kasperl
Copy link

kasperl commented Aug 4, 2014

Removed Oldschool-Milestone-Later label.

@DartBot DartBot added Type-Enhancement area-language Dart language related items (some items might be better tracked at github.com/dart-lang/language). labels Aug 4, 2014
@kevmoo kevmoo added P2 A bug or feature request we're likely to work on type-enhancement A request for a change that isn't a bug and removed accepted labels Feb 29, 2016
@munificent
Copy link
Member

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.

@munificent munificent added the closed-not-planned Closed as we don't intend to take action on the reported issue label Dec 19, 2016
@cedvdb
Copy link

cedvdb commented Jul 6, 2022

That's annoying when trying to replace a "primitive" (quoted) with their domain equivalent (subset). Is there any kind of proposal that would allow such a thing without killing the optimisations set in place ?

Something like Scala Values

https://docs.scala-lang.org/overviews/core/value-classes.html

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-language Dart language related items (some items might be better tracked at github.com/dart-lang/language). closed-not-planned Closed as we don't intend to take action on the reported issue P2 A bug or feature request we're likely to work on type-enhancement A request for a change that isn't a bug
Projects
None yet
Development

No branches or pull requests

7 participants