Skip to content

Enums as specified do not cover interop situations well #21966

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

Closed
DartBot opened this issue Dec 28, 2014 · 5 comments
Closed

Enums as specified do not cover interop situations well #21966

DartBot opened this issue Dec 28, 2014 · 5 comments
Labels
area-language Dart language related items (some items might be better tracked at github.com/dart-lang/language). closed-duplicate Closed in favor of an existing report type-enhancement A request for a change that isn't a bug

Comments

@DartBot
Copy link

DartBot commented Dec 28, 2014

This issue was originally filed by [email protected]


Enums as specified do not cover interop situations well where there's an expectation that an enum can be associated with a particular value (e.g. pnacl, http status messages with gaps in ranges, Windows messages, etc).

Please provide any additional information below.

Originally mentioned in comment to Issue #21416

@iposva-google
Copy link
Contributor

Added Area-Language, Triaged labels.

@DartBot
Copy link
Author

DartBot commented Dec 29, 2014

This comment was originally written by @mhausner


Note that in this rare case, you can simply use symbolic integer constants, e.g. const int INTERNAL_SERVER_ERROR = 500.

@gbracha
Copy link
Contributor

gbracha commented Dec 29, 2014

Enums were not intended to address this use case.


Set owner to @gbracha.
Added Accepted label.

@DartBot
Copy link
Author

DartBot commented Jan 9, 2015

This comment was originally written by [email protected]


Enums in C++, C#, TypeScript, and D map to ints:

http://msdn.microsoft.com/en-us/library/2dzy4k6e.aspx
http://msdn.microsoft.com/en-us/library/sbbt4032.aspx
http://www.typescriptlang.org/Handbook#basic-types-enum
http://dlang.org/enum.html

Protobufs are similar:

https://developers.google.com/protocol-buffers/docs/proto#enum

I'd prefer if Dart's enums were like that, too. If some external system considers MONDAY to be equal to 107 (real-word example), then you have to use that value, too. There is no way around it.

If you use constants, you lose the tooling benefits.

If you use metadata to glue the values to the enum, they get rather heavy and there are some glaring issues:

    import 'dart:mirrors';

    class Enum {
      final values;
      const Enum(this.values);
    }
    
    @­Enum(const [11, 22, 33])
    enum Foo {UNO, DOS, TRES}
    
    @­Enum(const {
      Bar.UNO: 111,
      Bar.DOS: 222
    })
    enum Bar {UNO, DOS}
    
    int valueOf(e) {
      var values = reflect(e).type.metadata.first.reflectee.values;
      if (values is List) {
        return values[e.index];
      }
      return values[e];
    }
    
    main() {
      print(valueOf(Foo.DOS)); // 22
      print(valueOf(Bar.DOS)); // 222
    }

@DartBot DartBot added Type-Enhancement area-language Dart language related items (some items might be better tracked at github.com/dart-lang/language). labels Jan 9, 2015
@kevmoo kevmoo added type-enhancement A request for a change that isn't a bug and removed accepted labels Feb 29, 2016
@lrhn lrhn added the closed-duplicate Closed in favor of an existing report label Sep 29, 2021
@lrhn
Copy link
Member

lrhn commented Sep 29, 2021

Duplicate of dart-lang/language#158

@lrhn lrhn marked this as a duplicate of dart-lang/language#158 Sep 29, 2021
@lrhn lrhn closed this as completed Sep 29, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-language Dart language related items (some items might be better tracked at github.com/dart-lang/language). closed-duplicate Closed in favor of an existing report type-enhancement A request for a change that isn't a bug
Projects
None yet
Development

No branches or pull requests

5 participants