Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enhancement: Enum #88

Closed
DartBot opened this issue Oct 12, 2011 · 19 comments
Closed

Enhancement: Enum #88

DartBot opened this issue Oct 12, 2011 · 19 comments
Assignees
Labels
area-language Deprecated: New language issues should be filed at https://github.com/dart-lang/language type-enhancement

Comments

@DartBot
Copy link

DartBot commented Oct 12, 2011

This issue was originally filed by carstenkl...@yahoo.de


I am missing a generic Enum<T> class like for example in Java.

Could this be included into the language please?

@DartBot
Copy link
Author

DartBot commented Oct 12, 2011

This comment was originally written by jat@google.com


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


Removed Type-Defect label.
Added Type-Enhancement, Area-Language, Triaged labels.

@gbracha
Copy link
Contributor

gbracha commented Oct 12, 2011

Set owner to @gbracha.

@DartBot
Copy link
Author

DartBot commented Dec 5, 2011

This comment was originally written by bwuest...@gmail.com


I find Java's advanced enumerations very powerful over the alternative of defining static final fields. In addition to the type checking they provide and being able to iterate over the entire collection of values, being able to define fields for each type really helps code maintainability for many scenarios in my opinion. I would love to see a similar construct in dart down the road. Has this feature been added to any schedule or is this still simply on the "wish list?"

@danrubel
Copy link

danrubel commented Dec 16, 2011

[anonymous user feedback]
Please add enums. Time to break out of c language hell

@DartBot
Copy link
Author

DartBot commented Mar 29, 2012

This comment was originally written by sorinm...@google.com


IMHO, Enums in Java cover two major language functionalities:

  1. Enforce type correctness. For example:

enum Gender {
  MALE,
  FEMALE
}

void createPerson(String name, Gender gender) { ... }

  1. Enclose functionality related to the type. For example:

enum UrlParameter {
  FIRST_NAME("firstName", true /* required /),
  BIRTHDAY("birthday", false /
required */);

  UrlParameter(String name, boolean isRequired) { ... }

  // ...

  boolean isRequired() {
    return isRequired;
  }
}

I feel that the second one is arguably leading to bad patterns (e.g. when Enums start making real work instead of delegating).

However, I feel that the first one (type safety) is necessary, otherwise the validity checks will end up being written by developers and executed at runtime.

@DartBot
Copy link
Author

DartBot commented Mar 29, 2012

This comment was originally written by ladicek@gmail.com


There are at least two other functions of enums: 1. communicating an API (if a function accepts only a restricted set of values, it's way better to make that argument an enum instead of an int), 2. restricting instantiability of a class (there can only be the specified instances of the enum class, no others).

@DartBot
Copy link
Author

DartBot commented Mar 29, 2012

This comment was originally written by sorinm...@google.com


@ladi...@gmail.com, I would like to add to your comments:

The "communicating an API" argument overlaps with what I was trying to explain by "type safety". Thank you for detailing this use case.

For restricting the instantiability of a class, my impression is that the Dart factory model should address this pattern better.

I guess the point I was trying to make was the following:
Since Dart is a young language, I agree that you don't want to get in it any features that have an awkward design (and will be hard to take back later). Enums in Java are quite complex to include in the first months of a language. But there is a core aspect to them (call it "type correctness" or "communicating an API"). This core aspect is a very powerful tool and I'd like to have it earlier rather than later.

@DartBot
Copy link
Author

DartBot commented Apr 22, 2012

This comment was originally written by rtimon...@gmail.com


What's wrong with

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

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

@DartBot
Copy link
Author

DartBot commented Apr 22, 2012

This comment was originally written by ladicek@gmail.com


What's wrong with

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

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

This 'if' is exactly what's wrong, you can't do a 'switch'. Enums are one of the few cases where switch is way better than if. Also, it's overly verbose, and would be even more verbose when you'd add const constructors, which would be necessary in a lot of times -- enum cases as compile time constants are a need.

@DartBot
Copy link
Author

DartBot commented Apr 22, 2012

This comment was originally written by amatiasq...@gmail.com


also with class inhetitance you can't limit the possible values with enum
you provide a set of values and nothing else will be accepted
El 22/04/2012 11:22, <dart@googlecode.com> escribi�

@DartBot
Copy link
Author

DartBot commented Apr 23, 2012

This comment was originally written by sorinm...@google.com


+1 on the previous two comments.

In addition to comment 10, I also believe there is value in being able to iterate through all possible values and validating that your code is handling all possible states, say in an unit test.

@anders-sandholm
Copy link
Contributor

anders-sandholm commented Apr 30, 2012

Added apr30-triage label.

@DartBot
Copy link
Author

DartBot commented Apr 30, 2012

This comment was originally written by carstenkl...@yahoo.de


For a starter, perhaps have a look at the enumjs project hosted over at github.com, which implements a type safe, enumerable enum that can be used in for example switch statements and for simple comparisons between enum constants.

See github.com/axnsoftware/enumjs.

It was implemented using coffeescript, but can easily be ported to for example dart.

@anders-sandholm
Copy link
Contributor

anders-sandholm commented May 1, 2012

Removed apr30-triage label.

@anders-sandholm
Copy link
Contributor

anders-sandholm commented May 1, 2012

Added triage1 label.

@anders-sandholm
Copy link
Contributor

anders-sandholm commented May 1, 2012

Let's up the priority on this one. Setting milestone to M1.


Added this to the M1 milestone.
Removed triage1 label.

@gbracha
Copy link
Contributor

gbracha commented May 1, 2012

Added Accepted label.

@DartBot
Copy link
Author

DartBot commented May 1, 2012

This comment was originally written by @seaneagan


One way to get enums without baking them into the language would be to define a "marker" interface possibly in a dart:enum library for enums to implement:

interface Enum {}

and then add the ability to mark classes as final (non-extensible). A class X which implements Enum would be checked by tools that it is final, has no public constructors, and defines a static values getter of type Set<X>:

final class Gender implements Enum {
  static final values = new Set<Gender>();

  static final MALE = new Gender.();
  static final FEMALE = new Gender.
();

  Gender._() => values.add(this);
}

Tools could also check in switch statements on expressions of type X, that there exist cases for all static final fields of X of type X.

This approach might allow for more flexibility in how to define enums.

@DartBot
Copy link
Author

DartBot commented May 1, 2012

This comment was originally written by @seaneagan


Actually, it would probably need to look more like the below due to lazy initialization of statics. This would be a good use case for Set literals:

final class Gender implements Enum {
  static final values = <Gender>[MALE, FEMALE];

  static final MALE = new Gender.();
  static final FEMALE = new Gender.
();

  Gender._();
}

dart-bot pushed a commit that referenced this issue Aug 11, 2021
copybara-service bot pushed a commit that referenced this issue May 13, 2022
Changes:
```
> git log --format="%C(auto) %h %s" 2fa188c..405fc79
 https://dart.googlesource.com/http_io.git/+/405fc79 Bump actions/checkout from 2 to 3 (#92)
 https://dart.googlesource.com/http_io.git/+/ccfb1ef Fix updated lints (#91)
 https://dart.googlesource.com/http_io.git/+/fcb94c0 Dart format with latest SDK (#90)
 https://dart.googlesource.com/http_io.git/+/b723cb6 Bump dart-lang/setup-dart from 0.3 to 1 (#89)
 https://dart.googlesource.com/http_io.git/+/ac00226 Add dependabot
 https://dart.googlesource.com/http_io.git/+/78956f5 Update LICENSE (#88)
 https://dart.googlesource.com/http_io.git/+/1715644 Migrate GitHub Actions (#86)
 https://dart.googlesource.com/http_io.git/+/5de81cd Update test API usage to non-deprecated members (#85)
 https://dart.googlesource.com/http_io.git/+/2cb230f Update formatting after dartfmt change
 https://dart.googlesource.com/http_io.git/+/c84afce test: remove unused, optional/positional argument in private function
 https://dart.googlesource.com/http_io.git/+/3adb2fd Fix lints (#81)
 https://dart.googlesource.com/http_io.git/+/e47fc8f lint cleanup (#80)
 https://dart.googlesource.com/http_io.git/+/5a3e178 Enable and fix lints (#79)
 https://dart.googlesource.com/http_io.git/+/771fb58 Bump min SDK for http_io (#78)
 https://dart.googlesource.com/http_io.git/+/fe5c4bf Fix return type in test functions
 https://dart.googlesource.com/http_io.git/+/0384af8 Travis: Update min tested SDK

```

Diff: https://dart.googlesource.com/http_io.git/+/2fa188caf7937e313026557713f7feffedd4978b~..405fc79233b4a3d4bb079ebf438bb2caf2f49355/
Change-Id: I1107e9dcdbb52bb3b22bd5abbcd347c5a83aea2d
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/244720
Reviewed-by: Nate Bosch <nbosch@google.com>
Commit-Queue: Devon Carew <devoncarew@google.com>
copybara-service bot pushed a commit that referenced this issue May 16, 2022
…997897

Changes:
```
> git log --format="%C(auto) %h %s" dd6fb5d..e496577
 https://dart.googlesource.com/external/github.com/google/webkit_inspection_protocol.dart.git/+/e496577 improve failure modes of ChromeConnection.getTabs (#88)
 https://dart.googlesource.com/external/github.com/google/webkit_inspection_protocol.dart.git/+/49acd16 upgrade to lints 2.0.0 (#87)
 https://dart.googlesource.com/external/github.com/google/webkit_inspection_protocol.dart.git/+/c34ebda rev to 1.0.1 (#84)
 https://dart.googlesource.com/external/github.com/google/webkit_inspection_protocol.dart.git/+/950d52a Update README.md
 https://dart.googlesource.com/external/github.com/google/webkit_inspection_protocol.dart.git/+/3dd20cc Update README.md (#83)
 https://dart.googlesource.com/external/github.com/google/webkit_inspection_protocol.dart.git/+/a79df73 depend on actions/checkout w/ a major version dep (#82)
 https://dart.googlesource.com/external/github.com/google/webkit_inspection_protocol.dart.git/+/9ffbd21 Bump dart-lang/setup-dart from 1.0 to 1.3 (#81)
 https://dart.googlesource.com/external/github.com/google/webkit_inspection_protocol.dart.git/+/dbc0fde Use more specific versions in the github action file (#79)
 https://dart.googlesource.com/external/github.com/google/webkit_inspection_protocol.dart.git/+/092c910 Bump actions/checkout from 2 to 3 (#78)
 https://dart.googlesource.com/external/github.com/google/webkit_inspection_protocol.dart.git/+/7156f1a Bump nanasess/setup-chromedriver from 1.0.5 to 1.0.7 (#77)
 https://dart.googlesource.com/external/github.com/google/webkit_inspection_protocol.dart.git/+/920a381 configure dependabot (#76)
 https://dart.googlesource.com/external/github.com/google/webkit_inspection_protocol.dart.git/+/73d61fa run tests in github actions (#74)
 https://dart.googlesource.com/external/github.com/google/webkit_inspection_protocol.dart.git/+/8a3b2b3 Update README.md (#73)
 https://dart.googlesource.com/external/github.com/google/webkit_inspection_protocol.dart.git/+/57ca5b0 enable the avoid_dynamic_calls lint (#72)
 https://dart.googlesource.com/external/github.com/google/webkit_inspection_protocol.dart.git/+/d10f668 Use package lints (#70)
 https://dart.googlesource.com/external/github.com/google/webkit_inspection_protocol.dart.git/+/da43164 add support for github actions (#69)
 https://dart.googlesource.com/external/github.com/google/webkit_inspection_protocol.dart.git/+/18620ab Delete .travis.yml

```

Diff: https://dart.googlesource.com/external/github.com/google/webkit_inspection_protocol.dart.git/+/dd6fb5d8b536e19cedb384d0bbf1f5631923f1e8~..e4965778e2837adc62354eec3a19123402997897/
Change-Id: I1052f9efedcb05e8dae815b926294ea6182c9560
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/244941
Reviewed-by: Anna Gringauze <annagrin@google.com>
Commit-Queue: Devon Carew <devoncarew@google.com>
copybara-service bot pushed a commit that referenced this issue Oct 21, 2022
args (https://github.com/dart-lang/args/compare/80d4abb..aaf671c):
  aaf671c  2022-10-20  Kevin Moore  Require Dart 2.18, update lints, update CI, add dependabot (#222)

async (https://github.com/dart-lang/async/compare/f3ed5f6..18a780e):
  18a780e  2022-10-20  Kevin Moore  Nit: fix analysis_options (#224)
  b6e7885  2022-10-20  Kevin Moore  Require Dart 2.18, latest lints, latest CI bits (#223)

lints (https://github.com/dart-lang/lints/compare/8294e56..16bdefe):
  16bdefe  2022-10-20  dependabot[bot]  Bump actions/checkout from 3.0.2 to 3.1.0 (#89)
  0fae4ee  2022-10-20  Devon Carew  update lint files docs (#88)

usage (https://github.com/dart-lang/usage/compare/9a98c89..fee1d9d):
  fee1d9d  2022-10-20  dependabot[bot]  Bump actions/checkout from 3.0.2 to 3.1.0 (#184)

webdev (https://github.com/dart-lang/webdev/compare/a02f073..81c5384):
  81c5384  2022-10-20  Nate Bosch  Migrate webdev tests to null safety (#1761)

Change-Id: I238d46cb5b24d64788862f06dc903a2cd2312725
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/265140
Auto-Submit: Devon Carew <devoncarew@google.com>
Reviewed-by: Nate Bosch <nbosch@google.com>
Commit-Queue: Nate Bosch <nbosch@google.com>
copybara-service bot pushed a commit that referenced this issue Oct 26, 2022
Revisions updated by `dart tools/rev_sdk_deps.dart`.

markdown (https://github.com/dart-lang/markdown/compare/93d0eee..9b61871):
  9b61871  2022-10-25  Kevin Moore  Make helper class private that should not have been exposed (#476)
  299964e  2022-10-26  le.chang  Return list for link nodes creation (#452)
  aee6a40  2022-10-25  Kevin Moore  validate code coverage on CI (#474)
  88f3f8a  2022-10-25  Zhiguang Chen  Fix html entity and numeric character references (#467)

source_span (https://github.com/dart-lang/source_span/compare/ff03af1..d1d47e5):
  d1d47e5  2022-10-25  Kevin Moore  Require Dart 2.18, update lints, latest pkg:lints, add dependabot (#88)

string_scanner (https://github.com/dart-lang/string_scanner/compare/2d84b16..10435a4):
  10435a4  2022-10-25  Kevin Moore  Update minimum SDK, latest pkg:lints, update CI actions (#48)

Change-Id: I94dbb0830600cb5304aaae16b2ae715bfc71411e
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/265641
Auto-Submit: Devon Carew <devoncarew@google.com>
Commit-Queue: Nate Bosch <nbosch@google.com>
Reviewed-by: Nate Bosch <nbosch@google.com>
This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-language Deprecated: New language issues should be filed at https://github.com/dart-lang/language type-enhancement
Projects
None yet
Development

No branches or pull requests

5 participants