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
Comments
Added Area-Language, Triaged labels. |
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. |
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. |
Removed this from the Later milestone. |
Removed Oldschool-Milestone-Later label. |
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. |
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 |
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.
The text was updated successfully, but these errors were encountered: