Program Tip

Dart는 열거를 지원합니까?

programtip 2020. 10. 21. 21:23
반응형

Dart는 열거를 지원합니까?


Dart는 열거를 지원합니까? 예를 들면 :

enum myFruitEnum { Apple, Banana }

문서에 대한 간단한 검색은 아니오를 제안합니다.


1.8 부터 다음과 같이 열거 형을 사용할 수 있습니다.

enum Fruit {
  apple, banana
}

main() {
  var a = Fruit.apple;
  switch (a) {
    case Fruit.apple:
      print('it is an apple');
      break;
  }

  // get all the values of the enums
  for (List<Fruit> value in Fruit.values) {
    print(value);
  }

  // get the second value
  print(Fruit.values[1]);
}

1.8 이전의 이전 접근 방식 :

class Fruit {
  static const APPLE = const Fruit._(0);
  static const BANANA = const Fruit._(1);

  static get values => [APPLE, BANANA];

  final int value;

  const Fruit._(this.value);
}

클래스 내의 이러한 정적 상수는 컴파일 시간 상수이며, 이제이 클래스는 예를 들어 다음 switch명령문 에서 사용할 수 있습니다 .

var a = Fruit.APPLE;
switch (a) {
  case Fruit.APPLE:
    print('Yes!');
    break;
}

r41815를 사용하면 Dart가 기본 Enum 지원을 받았으며 http://dartbug.com/21416을 참조 하고 다음과 같이 사용할 수 있습니다.

enum Status {
  none,
  running,
  stopped,
  paused
}

void main() {
  print(Status.values);
  Status.values.forEach((v) => print('value: $v, index: ${v.index}'));
  print('running: ${Status.running}, ${Status.running.index}');
  print('running index: ${Status.values[1]}');
}

[Status.none, Status.running, Status.stopped, Status.paused]
값 : Status.none, 인덱스 : 0
값 : Status.running, 인덱스 : 1
값 : Status.stopped, 인덱스 : 2
값 : Status.paused, 인덱스 : 3
실행 : Status.running, 1
실행 인덱스 : Status.running

A limitation is that it is not possibly to set custom values for an enum item, they are automatically numbered.

More details at in this draft https://www.dartlang.org/docs/spec/EnumsTC52draft.pdf


This and this may be the answers on your question:

... for the technology preview it was decided to leave it out and just 
use static final fields for now. It may be added later.

You can still do something like this:

interface ConnectionState { }
class Connected implements ConnectionState { }
class Connecting implements ConnectionState { }
class Disconnected implements ConnectionState { }

//later
ConnectionState connectionState;
if (connectionState is Connecting) { ... }

wich is in my opinion more clear for use. It's a bit more difficult for programming the application structure, but in some cases, it's better and clear.


Enumeration should be available in the future. But until Enum has landed you can do something like :

class Fruit {
  static final APPLE = new Fruit._();
  static final BANANA = new Fruit._();

  static get values => [APPLE, BANANA];

  Fruit._();
}

how about this approach:

class FruitEnums {
  static const String Apple = "Apple";
  static const String Banana = "Banana";
}

class EnumUsageExample {

  void DoSomething(){

    var fruit = FruitEnums.Apple;
    String message;
    switch(fruit){
      case(FruitEnums.Apple):
        message = "Now slicing $fruit.";
        break;
      default:
        message = "Now slicing $fruit via default case.";
        break;
    }
  }
}

참고URL : https://stackoverflow.com/questions/13899928/does-dart-support-enumerations

반응형