在 Dart 中,List.from 和 .of 之间以及 Map.from 和 .of 之间有什么区别? [英] In Dart, what's the difference between List.from and .of, and between Map.from and .of?
问题描述
在 Dart 中,List.from
和 List.of
之间,以及 Map.from
和 Map 之间有什么区别.的
?他们的文档并不完全清楚:
In Dart, what's the difference between List.from
and List.of
, and between Map.from
and Map.of
? Their documentation is not totally clear:
/**
* Creates a [LinkedHashMap] instance that contains all key/value pairs of
* [other].
*
* The keys must all be instances of [K] and the values of [V].
* The [other] map itself can have any type.
*
* A `LinkedHashMap` requires the keys to implement compatible
* `operator==` and `hashCode`, and it allows `null` as a key.
* It iterates in key insertion order.
*/
factory Map.from(Map other) = LinkedHashMap<K, V>.from;
/**
* Creates a [LinkedHashMap] with the same keys and values as [other].
*
* A `LinkedHashMap` requires the keys to implement compatible
* `operator==` and `hashCode`, and it allows `null` as a key.
* It iterates in key insertion order.
*/
factory Map.of(Map<K, V> other) = LinkedHashMap<K, V>.of;
/**
* Creates a list containing all [elements].
*
* The [Iterator] of [elements] provides the order of the elements.
*
* All the [elements] should be instances of [E].
* The `elements` iterable itself may have any element type, so this
* constructor can be used to down-cast a `List`, for example as:
* ```dart
* List<SuperType> superList = ...;
* List<SubType> subList =
* new List<SubType>.from(superList.whereType<SubType>());
* ```
*
* This constructor creates a growable list when [growable] is true;
* otherwise, it returns a fixed-length list.
*/
external factory List.from(Iterable elements, {bool growable: true});
/**
* Creates a list from [elements].
*
* The [Iterator] of [elements] provides the order of the elements.
*
* This constructor creates a growable list when [growable] is true;
* otherwise, it returns a fixed-length list.
*/
factory List.of(Iterable<E> elements, {bool growable: true}) =>
new List<E>.from(elements, growable: growable);
差异是否与泛型有关?也许 .from
工厂允许您更改列表的类型,而 .of
工厂则不允许?我来自 Java 背景,适用于类型擦除,也许类型在 Dart 中被具体化,并且您不能使用强制转换或原始类型来更改列表/映射类型?
Is the difference related to generics? Maybe the .from
factories let you change the type of the list, while the .of
ones do not? I come from a Java background, which works with type erasure, and maybe types are reified in Dart and you cannot use casts or raw types to change list/map types?
推荐答案
from
和 of
方法的重要区别是后者有类型注解,前者有类型注解不要.由于 Dart 泛型被具体化并且 Dart 2 是强类型的,因此这是确保 List/Map
正确构造的关键:
The important difference between the from
and of
methods are that the latter have type annotations and the former do not. Since Dart generics are reified and Dart 2 is strongly typed, this is key to both ensuring the List/Map
is correctly constructed:
List<String> foo = new List.from(<int>[1, 2, 3]); // okay until runtime.
List<String> bar = new List.of(<int>[1, 2, 3]); // analysis error
并确保正确推断类型:
var foo = new List.from(<int>[1, 2, 3]); // List<dynamic>
var bar = new List.of(<int>[1, 2, 3]); // List<int>
在 Dart 1 中,类型是完全可选的,因此许多 API 是无类型或部分类型的.List.from
和 Map.from
是很好的例子,因为传入它们的 Iterable/Map
没有类型参数.有时 Dart 可以弄清楚这个对象的类型应该是什么,但有时它只是以 List
或 Map
结束.
In Dart 1 types were completely optional, so many APIs were untyped or partially typed. List.from
and Map.from
are good examples, since the Iterable/Map
passed into them doesn't have a type parameter. Sometimes Dart can figure out what the type of this object should be, but sometimes it just ended up as List<dynamic>
or Map<dynamic, dynamic>
.
在 Dart 2 中,dynamic
类型从顶部(对象)和底部(空)类型更改为仅顶部类型.因此,如果您在 Dart 1 中意外创建了 List
,您仍然可以将其传递给需要 List
的方法.但是在 Dart 2 中 List
几乎和 List
一样,所以这会失败.
In Dart 2 the type dynamic
was changed from being both a top (Object) and bottom (null) type to only being a top type. Thus if you created a List<dynamic>
accidentally in Dart 1 you could still pass it to a method which required a List<String>
. But in Dart 2 List<dynamic>
is almost the same as List<Object>
, so this would fail.
如果您使用的是 Dart 2,则应始终使用这些 API 的类型化版本.为什么旧的还存在,那里有什么计划?我真的不知道.我猜他们会随着时间的推移和 Dart 1 的其余部分一起被淘汰.
If you are using Dart 2, you should always use the typed version of these APIs. Why do the old ones still exist, and what are the plans there? I don't really know. I would guess they would be phased out over time, along with the rest of the Dart 1.
这篇关于在 Dart 中,List.from 和 .of 之间以及 Map.from 和 .of 之间有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!