gpt4 book ai didi

dart - 在 Dart2 中,用于泛型的正确 "anything"类型是什么?

转载 作者:行者123 更新时间:2023-12-01 19:15:30 24 4
gpt4 key购买 nike

AngularDart 有一个名为 AppView 的类,即abstract class AppView<T> {} .

为每个用@Component注释的类生成一个(至少)。 :

// file.dart
@Component(...)
class DashboardComponent {}


// file.template.dart (Generated)
class ViewDashboardComponent extends AppView<DashboardComponent> {}

我在框架的其他地方有代码,不关心这 T类型是。我对 Dart 2 的“正确”“任何”类型有点困惑。例如,我可以使用:

  • AppView
  • AppView<dynamic>
  • AppView<Object>
  • AppView<Null>
  • AppView<void>

我认为其中不止一个会“起作用”。但在这种情况下使用哪个是“正确的”呢?

最佳答案

您应该可以使用 AppView (或 AppView<dynamic> )几乎任何地方。我可以想到两个例子,这会给你带来麻烦:

  1. 如果您实例化一个AppView,您肯定需要该类型参数。如果您不这样做,则会看到以下错误:

    $ cat a.dart
    void main() {
    List<dynamic> a = ["one", "two", "three"];
    List<String> b = a;
    }
    $ dart --preview-dart-2 a.dart
    Unhandled exception:
    type 'List' is not a subtype of type 'List<String>' where
    List is from dart:core
    List is from dart:core
    String is from dart:core

    #0 main (file:///Users/sam/a.dart:3:20)
    #1 _startIsolate.<anonymous closure> (dart:isolate/isolate_patch.dart:279:19)
    #2 _RawReceivePortImpl._handleMessage (dart:isolate/isolate_patch.dart:165:12)
  2. 如果您分配一个闭包给一个需要一个或多个涉及 T 的类型化参数的闭包的站点,您将看到“使用动态作为底部”静态错误(来自分析器),也可能是运行时错误:

    $ cat f.dart                                                       

    void main() {
    List a = <String>["one", "two", "three"];
    a.map((String s) => s.toUpperCase());

    List b = ["one", "two", "three"];
    b.map((String s) => s.toUpperCase());
    }
    $ dart --preview-dart-2 f.dart
    f.dart:3:9: Error: A value of type '(dart.core::String) → dart.core::String' can't be assigned to a variable of type '(dynamic) → dynamic'.
    Try changing the type of the left hand side, or casting the right hand side to '(dynamic) → dynamic'.
    a.map((String s) => s.toUpperCase());
    ^
    f.dart:6:9: Error: A value of type '(dart.core::String) → dart.core::String' can't be assigned to a variable of type '(dynamic) → dynamic'.
    Try changing the type of the left hand side, or casting the right hand side to '(dynamic) → dynamic'.
    b.map((String s) => s.toUpperCase());
    ^

(我不确定任何 Dart 工具是否具有完整的 Dart 2 运行时和编译时语义,因此这可能会略有变化。)

在这些情况下,最好使用 generic classes , generic methods ,和 generic typedefs对于给定范围,封装对象类型参数的值可能是什么。

我怀疑 dynamic 之间存在差异和Object在 Dart 2 中,我认为 Günter 在他的回复中介绍了这一点,但如果您的代码“不关心这个 T 类型是什么”,那么您可能不会调用组件上的任何方法。

编辑:无效

AppView<void>在这种情况下可能是一个不错的选择,作为实际检查您实际上从未接触过底层组件(对象可能会起到相同的作用)。了解如何允许我们访问 List<void> 的属性但不是元素的属性:

$ cat g.dart
void main() {
var c = <String>["one", "two", "three"];
fn(c);
fn2(c);
}

int fn(List<void> list) => list.length;

int fn2(List<void> list) => list.first.length;
$ dart --preview-dart-2 g.dart
g.dart:9:40: Error: The getter 'length' isn't defined for the class 'void'.
Try correcting the name to the name of an existing getter, or defining a getter or field named 'length'.
int fn2(List<void> list) => list.first.length;
^

关于dart - 在 Dart2 中,用于泛型的正确 "anything"类型是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49308384/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com