Comments (5)
It's expected and specified behavior.
The analyzer doesn't warn, because it's perfectly valid code.
The inherent unsoundness in covariant generics is so deep in the language that it's impossible to warn about it, because that would make almost all uses of (for example, but not limited to) List.add
give a warning.
It might be possible to locally deduce that you are casting a List<int>
to List<Object>
, and shortly after doing .add(x)
on it with something that's not an int
. But it's also incredibly easy to do that without the analyzer having any chance of recognizing it.
from sdk.
Because your code is not type safe. And nor is List
.
It's the same error you'll get from
List<Object?> list = <C>[];
list.add(B()); // Type error, B is not a C
The function test
takes a Holder
, aka Holder<Object?>
.
That contains some List<Content<T>>
, deleted you haven't shown the type. I'm assuming it's a List<Content<C>>
, but from where your code is looking, it has static type List<Content<Object?>>
If you try to add a Content<B>
to that, it fails, as it should.
If you do .toList()
on it, you get another List<Content<C>>
with static type List<Content<Object?>>
, and adding still fails.
If you do var list = List.of(...);
, you create a new List<Content<Object?>>
with the same elements. That's safe, and you can safely add to it. That is, the static element type becomes the runtime element type of the newly created list.
That's why it works.
from sdk.
Because your code is not type safe. And nor is
List
.It's the same error you'll get from
List<Object?> list = <C>[]; list.add(B()); // Type error, B is not a CThe function
test
takes aHolder
, akaHolder<Object?>
. That contains someList<Content<T>>
, deleted you haven't shown the type. I'm assuming it's aList<Content<C>>
, but from where your code is looking, it has static typeList<Content<Object?>>
If you try to add a
Content<B>
to that, it fails, as it should.If you do
.toList()
on it, you get anotherList<Content<C>>
with static typeList<Content<Object?>>
, and adding still fails.If you do
var list = List.of(...);
, you create a newList<Content<Object?>>
with the same elements. That's safe, and you can safely add to it. That is, the static element type becomes the runtime element type of the newly created list. That's why it works.
Thanks for answer but I still don't get it. toList()
is a shortcut for List.of()
(basically the same operation) then why behaviour differs?
from sdk.
If list
is an instance of List<E>
, then list.toList()
works like List<E>.of(list)
, no matter what the static type of list
is.
Doing List.of(list)
infers the type of the created list from the static type of list
, because the compiler infers the missing type argument to List
. This is List<T>.of(list)
for some T
, which is inferred because you didn't write it.
Which means:
List<Object> list = <int>[1]; // Valid, up-cast.
var list2 = list.toList(); // Same as `List<int>.of(list);`, called by list itself, which knows it's an `int` list.
var list3 = List.of(list); // Same as `List<Object>.of(list);`, inferred from static type.
Those two calls are not the same.
from sdk.
If
list
is an instance ofList<E>
, thenlist.toList()
works likeList<E>.of(list)
, no matter what the static type oflist
is.Doing
List.of(list)
infers the type of the created list from the static type oflist
, because the compiler infers the missing type argument toList
. This isList<T>.of(list)
for someT
, which is inferred because you didn't write it.Which means:
List<Object> list = <int>[1]; // Valid, up-cast. var list2 = list.toList(); // Same as `List<int>.of(list);`, called by list itself, which knows it's an `int` list. var list3 = List.of(list); // Same as `List<Object>.of(list);`, inferred from static type.Those two calls are not the same.
So is that an expected behaviour?
Why doesn't an analyzer warn about it or something?
from sdk.
Related Issues (20)
- What do you keep some asynchronous file I/O methods that you recommend avoiding in your lint rules? HOT 1
- Create an issue HOT 1
- [dart2wasm] crashing dart2wasm with a flame demo HOT 7
- Minor: Maybe add a note on usage of the `Random` class HOT 1
- add FileSystemEntity pathToFileSystemEntity(String path) to the standard library?
- [CFE] No error if type parameter uses `covariant` keyword HOT 1
- Invalid constant causes analysis server to stop updating problems view HOT 1
- [dart:js_interop] ExternalDartReference should have a generic argument. HOT 2
- Failures on Add a pass for suggesting elements that are not yet imported...Update clang to 0f61051f541a5b8cfce25c84262dfdbadb9ca688. HOT 4
- "Bad state: WebSocket is closed" on iso-stress-linux bot HOT 1
- [Process] Reexamination of Dart SDK Breaking Change Process HOT 1
- should ignore statements implicitly apply to augmentations? HOT 7
- `Vector2.clone()` performance hit for x86-64 and ARM64? [MOVED] HOT 4
- Incorrect unreachable case warning with nullable matched value type and list pattern HOT 3
- Allow abstract classes to be instantiated given definitions of all abstract methods HOT 1
- [ddc] Make it possible to move all `dart.applyMixin()` calls to a library link method
- [ddc] Reduce the special cases and assuptions during compilation of `dart:` libraries
- [ddc] Make it possible to move all peer native type extension operations to a library link method
- Proposal: Have `dart run <file.dart>` do resolution HOT 17
- [dart:js_interop] Can't pass an function reference to external function when assertions are enabled. HOT 1
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.