Comments (4)
Correct. The occurrence of T
in
final num Function(T)? method;
is contravariant, while the T
in the type itself is (unsafely) covariant.
Because of that, the code that reads the property mention.method
, where mention
has static type B<A>
, and the read expression therefore static type num Function(A)
, must be prepared for the value to be, fx, a num Function(A2)
, which is-not-a num Function(A)
.
To preserve soundness, the guarantee that the value of an expression satisfies the static type, the compiler inserts a type check that throws if the value isn't sound.
Basically it makes the read expression into (mention.method as num Function(A))
.
Which then fails at runtime, as intended.
If it hadn't thrown there, the next statement could've been method(A())
, which wouldn't be sound.
If you change the method
getter to a method:
final num Function(T) _method;
num method(T value) => _method(value);
then the reading becomes type safe, because the reader is now using the same type for T
, but calling the method may throw at runtime.
There is just no way to allow an actual num Function(A2)
to be successfully called with a A()
value.
from sdk.
See dart-lang/language#297 for some background info about the treatment of occurrences of type variables in positions that disagree with their variance.
See dart-lang/language#524 for a proposal about statically checked variance (covariance, contravariance, invariance). If we get that then you can use this:
class B<in T extends A> {
const B({this.method});
final num Function(T)? method;
}
.. and then B<A2>
will be a supertype of B<A>
. You can vote for dart-lang/language#524 if you wish to support the addition of a statically checked kind of variance to Dart.
from sdk.
I think the problem is that the mention
is of type B<A>
, which is actually a B<A2>
type. Therefore, I get an error since I won't be able to pass an instance of A
, but rather an instance of A2
.
from sdk.
Thank you for the clarification. Anyway, I did something similar with manual casting:
final num Function(T) _method;
num method(A value) => _method(value as T);
But your solution is better.
from sdk.
Related Issues (20)
- Reproducible Builds HOT 13
- `--print-dtd` flag not working for `dart run` HOT 1
- Weird type inference issue HOT 17
- Extension type implementing Stream can't be used as a return types of a async* function HOT 3
- Null check exceptions during json serialization during dry run HOT 1
- Extension type doesn't extend `Object` HOT 2
- Adding package http makes dart run really slow HOT 26
- Analyzer Feedback from IntelliJ
- Create an issue HOT 1
- Incorrect error messages while parsing (non-existent) logical operators `or`, `and`, and `xor` HOT 2
- No error in the analyzer to augment different type
- No error in the analyzer if augmenting type declares different number of parameters
- No error in the analyzer if augment class goes before original class HOT 2
- Hot reload test suite crashes test bots when parsing results logs
- [dart2js] Incorrect code generated in body of a function used as a .call getter
- [ddc] flaky behavior in for-loop eval test HOT 2
- evaluate performance for multi-option contexts HOT 1
- CFE allows trailing comma in catch clause
- Analyzer Feedback from IntelliJ
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 sdk.