Dart runtimeType检查switch语句 [英] Dart runtimeType checking in switch statement

查看:2234
本文介绍了Dart runtimeType检查switch语句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现一些奇怪的行为试图检查Dart中某些对象的运行时类型。
让我们举个简单的例子:

I have found some strange behaviors trying to check the runtime type of some objects in Dart. Let's make a simple example:

main(List<String> args) {
  List<Map> l1 = new List<Map>();
  l1.add(new Map<String, int>());
  List<int> l2 = ['Start', 'Stop'];
  checkType(l1.runtimeType);
  checkType(l1[0].runtimeType);
  checkType(l2.runtimeType);
}

checkType(Type type) {

  switch(type) {
    case List:
      print('it is a List!');
      break;
    case Map:
      print('it is a List!');
      break;
    default:
      print('Unmanaged type $type');
      break;
  }
}

此程序具有以下输出:

非托管类型列表< Map>
非托管类型 _InternalLinkedHashMap code>
非托管类型列表

第一种情况不能在switch语句中检查因为如果我尝试设置一个 List< Map> 的情况下,我得到错误:在常量表达式中,这个操作符的操作数必须是'
第二个不能匹配,因为使用_InternalLinkedHashMap在一个case中得到以下错误:case表达式必须是常量。

The first case cannot be checked in a switch statement because if I try to set a "List<Map>" case I get the error: in constant expressions, operand(s) of this operator must be of type 'num' The second cannot be matched because using the _InternalLinkedHashMap in a case gets the following error: Case expression must be constant.

在最后一种情况下,我已经定义了列为int列表( List< int> ),但系统忽略它并将其视为一个简单的列表。

In the last case I have defined the List as a List of ints (List<int>), but the system ignores it and considers it as a simple List. I think that this is misleading and such a declaration should be forbbiden.

任何帮助/建议?

推荐答案

如果你想根据对象的类型来做流控制,你实际上想根据对象的类是否实现了一个接口它的运行时类型是。这是类型测试运算符 是!的用途。

If you want to do flow control based on object's type, you actually want to do it based on whether an object's class implements an interface, not what it's run time type is. This is what the Type Test Operators is and is! are for.

请记住,在Dart中,类也是一个接口,因此您可以测试一个对象是否是一个特定类。

Remember that in Dart a class is also an interface, so you can test if an object is a specific class.

class Something {
 ...
}

var s = new Something();
print(s is Something);     // true

请注意,我们往往认为是类,例如 List 映射不是类,它们是接口。任何返回这样的实例(包括构造函数)事实上都返回一个实现接口的类。

Note that things we tend to think of as 'classes' such as List an Map, are not classes, they are interfaces. Anything that returns a instance of such (including constructors) in fact returns a class that implements the interface.

你可以使用泛型,但要小心。

You can use generics, but be careful.

void main() {

  var a = [1, 2, 'three'];
  print(a is List);          // True
  print(a is List<int>);     // True!!!!
  print(a is List<String>);  // True!!!!

  var b = new List<int>.from([1, 2, 3]);
  print(b is List);          // True
  print(b is List<int>);     // True
  print(b is List<String>);  // False
}

类可以通过显式实现来实现接口,一个实现它的类,或者通过一个混合。

A class can implement an interface via explicitly implementing it, inheriting from a class that implements it, or through a mix-in.

class Base {
  void a() {}
}

class Mix {
  void b() {}
}

class Mixed extends Base with Mix {} 

class Explicit implements Base {
  void a() {}  
}

void main() {

  var c = new Mixed();
  print(c is Mixed);         // True
  print(c is Base);          // True
  print(c is Mix);           // True

  var d = new Explicit();
  print(d is Base);          // True
}

这篇关于Dart runtimeType检查switch语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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