如何在 Dart 中执行运行时类型检查? [英] How to perform runtime type checking in Dart?

查看:43
本文介绍了如何在 Dart 中执行运行时类型检查?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Dart 规范说明:

具体化的类型信息在运行时反映对象的类型,并且可能总是被动态类型检查构造(其他语言中instanceOf、casts、typecase等的类似物).

Reified type information reflects the types of objects at runtime and may always be queried by dynamic typechecking constructs (the analogs of instanceOf, casts, typecase etc. in other languages).

听起来不错,但没有类似 instanceof 的运算符.那么我们如何在 Dart 中执行运行时类型检查呢?有可能吗?

Sounds great, but there is no instanceof-like operator. So how do we perform runtime type-checking in Dart? Is it possible at all?

推荐答案

instanceof-operator 在 Dart 中称为 is.该规范对普通读者并不完全友好,因此现在最好的描述似乎是 http://www.dartlang.org/articles/optional-types/.

The instanceof-operator is called is in Dart. The spec isn't exactly friendly to a casual reader, so the best description right now seems to be http://www.dartlang.org/articles/optional-types/.

这是一个例子:

class Foo { }

main() {
  var foo = new Foo();
  if (foo is Foo) {
    print("it's a foo!");
  }
}

这篇关于如何在 Dart 中执行运行时类型检查?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆