Comments (2)
the type argument for
Foo
could logically be inferred asdouble
andint
respectively based on the constructor parameters.
Not really. Since num
is a valid solution, it's not obvious that using double
/int
is a better solution.
It is in this case, but it might as well have not been. And if there is more than one constructor, then it becomes even less obvious which type relations should be constraints, and what should not.
But how far should it go?
abstract class S<T extends num> {
abstract final T value;
}
class T extends S {
final value;
T([this.value = 1]);
}
In this case one could (maybe) infer that final value;
should inherit the type int
to match the default value, so S
should be S<int>
.
But you don't know the type of 1
before you know the type of value
. If the type of value
was double
, the 1
would have had type double
too, so this is actually a cyclic dependency in the type inference algorithm.
There is a reason Dart decided to require the superclass types to be determined before trying to do type inference on members.
from language.
Thanks @lrhn for the clear explanation 🙏.
The inference algorithm I described can indeed become very complex when multiple constructors are involved, and in those cases (among others), I assume that might infer differently for what people would want.
Making your example clearer in practice:
abstract class S<T extends num> {
abstract final T value;
}
class T extends S/*<int> assuming value is inferred as int */ {
T([this.value = 1]);
int getInt() => value;
}
void main() {
final foo = T(2.0); // Error: value is already inferred as int, so we can't accept a double here. T cannot be both an int and a double.
}
I guess this inference algorithm would only be beneficial if, as in my example, the value is constant within the subclass. Such an edge case might not be a good reason to change the current behavior.
from language.
Related Issues (20)
- Promote `obj` to non-null when checking `obj?.field == value` HOT 3
- Trailing Parameter HOT 6
- Dart Implicit Static Access HOT 5
- How do we handle deferred imports in enhanced parts? HOT 1
- Simple parts with imports HOT 6
- Expressions that are "as constant as possible" HOT 20
- Switching on bounded generic types HOT 5
- Add AI issue summary HOT 9
- Type parameters with default value for classes HOT 7
- Reintroduce varargs parameter in functions (in limited capacity) HOT 7
- implement an 'implies' logical operator HOT 9
- Some "reach the end" errors are not reported. Fix it, or twist the spec? HOT 11
- Allow `case` condition in return statement HOT 3
- Why can't I write a guard clause for each pattern in the guard clause of pattern matching? HOT 6
- Add `default` Keyword and New `::default` Operator for Using Default Parameters HOT 1
- Generic type as function return type can't work HOT 2
- Trying to get `this` type is incredibly unweildy. Ref: `Self` from Rust HOT 1
- Tree-shaking not producing the same result depending on whether Dart code is compiled as Javascript or Android binary HOT 2
- Making `redeclare` a language feature? HOT 1
- Custom generators (like `async`, `async*` and `sync*`) HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from language.