在Dart中,使用设置表达式的Switch语句

huangapple go评论79阅读模式
英文:

Switch statement in dart with set expression

问题

以下是翻译好的内容:

这里有一个带有switch语句的简单的Dart程序。

void main() {
  var list = [1, 2];
  var set = <int>{1, 2};
  var map = <int, int>{1: 2, 3: 4};
   
  switch (list) {
    case 1:
      print(1);
      break;
    case [1, 2]:
      print(2);
      break;
    case const <int>{1, 2}:
      print(3);
      break;
    case <int, int>{1: 2, 3: 4}:
      print(4);
      break;
  }
}

根据我了解,Dart语言中的switch语句不会为每个情况检查==,而是进行模式匹配。

在上面的代码中,当表达式是switch(list)时,控制台打印出2。当它是switch(set)时,控制台不会打印任何内容,而对于switch(map),控制台打印出4

问题:

  1. 为什么当表达式是switch(set)时,switch不打印3
  2. 为什么Set需要const,而ListMap不需要?
    如果我在Set的情况下删除const,我会收到错误消息:

常量模式的表达式必须是有效的常量。

英文:

Here is a simple dart program with a switch statement.

void main() {
  var list = [1, 2];
  var set = &lt;int&gt;{1, 2};
  var map = &lt;int, int&gt; {1: 2, 3: 4};
   
  switch (list) {
    case 1:
      print(1);
      break;
    case [1, 2]:
      print(2);
      break;
    case const &lt;int&gt; {1, 2}:
      print(3);
      break;
    case &lt;int, int&gt;{1: 2, 3: 4}:
      print(4);
      break;
  }
}

As I understand in the dart lang, the switch statement does not check == for every case, but rather it does pattern matching.

In the above code, when the expression is switch(list), the console prints 2. When it is switch(set), the console does not print anything, and for switch(map), the console prints 4.

Questions:

  1. Why when the expression is switch(set), the switch does not print 3?
  2. Why const is required for Set, and not for List and Map?
    If I remove const in the set case, I get an error message :
    >The expression of a constant pattern must be a valid constant.

答案1

得分: 0

以下是已翻译好的内容:

Dart支持以下类型的模式:

请注意,ListMap 都具有内置的模式类型,而 Set 则没有。

然而,还有一个常量模式,允许使用 case const &lt;int&gt; {1, 2}:

另请考虑以下内容:

void main() {
  print(const &lt;int&gt; {1, 2} == &lt;int&gt; {1, 2}); // false
  print(const &lt;int&gt; {1, 2} == const &lt;int&gt; {1, 2}); // true
}

因此,考虑到这一点,以下代码:

void main() {
  var list = [1, 2];
  var set = const &lt;int&gt;{1, 2}; // 添加 const
  var map = &lt;int, int&gt; {1: 2, 3: 4};
   
  switch (set) {
    case 1:
      print(1);
      break;
    case [1, 2]:
      print(2);
      break;
    case const &lt;int&gt; {1, 2}:
      print(3);
      break;
    case &lt;int, int&gt;{1: 2, 3: 4}:
      print(4);
      break;
  }
}

会打印出 3

英文:

Dart supports the following types of patterns:

Notice that both List and Map have built-in pattern types, while Set does not.

There is however a constant pattern, which allows for case const &lt;int&gt; {1, 2}:.

Also consider this:

void main() {
  print(const &lt;int&gt; {1, 2} == &lt;int&gt; {1, 2}); // false
  print(const &lt;int&gt; {1, 2} == const &lt;int&gt; {1, 2}); // true
}

So with that in mind, the following:

void main() {
  var list = [1, 2];
  var set = const &lt;int&gt;{1, 2}; // add const
  var map = &lt;int, int&gt; {1: 2, 3: 4};
   
  switch (set) {
    case 1:
      print(1);
      break;
    case [1, 2]:
      print(2);
      break;
    case const &lt;int&gt; {1, 2}:
      print(3);
      break;
    case &lt;int, int&gt;{1: 2, 3: 4}:
      print(4);
      break;
  }
}

prints 3.

huangapple
  • 本文由 发表于 2023年6月27日 18:23:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/76563905.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定