xsahil03x / super_enum

Create super-powered dart enums similar to sealed classes in Kotlin
https://pub.dev/packages/super_enum
MIT License
116 stars 13 forks source link

Enums with Value #21

Closed sinadarvi closed 4 years ago

sinadarvi commented 4 years ago

the "extension"s has been added since Dart 2.6 so it would be great if we have Enums with default values. for example:

enum Country {
  UnitedArabEmirates,
  Argentina,
  Austria,
  Australia,
  Belgium,
  Bulgaria,
  Brazil,
}

extension CountryExtension on Country {
  String getValue() {
    switch (this) {
      case Country.UnitedArabEmirates:
        return "ae";
      case Country.Argentina:
        return "ar";
      case Country.Austria:
        return "at";
      case Country.Australia:
        return "au";
      case Country.Belgium:
        return "be";
      case Country.Bulgaria:
        return "bg";
      case Country.Brazil:
        return "br";
      default:
        return "COUNTRY NOT FOUND";
    }
  }
}

main(){
    var au = Country.Australia;
    print('the country code of Australia is ${au}');
}

as I represented, we can do it our selves but it would be nice if we can define that like this:

@superEnum
enum Country {
  @object(value: "ae")
  UnitedArabEmirates,
  @object(value: "ar")
  Argentina,
  @object(value: "at")
  Austria,
  @object(value: "au")
  Australia,
  @object(value: "be")
  Belgium,
  @object(value: "bg")
  Bulgaria,
  @object(value: "br")
  Brazil,
}

main(){
    var au = Country.Australia;
    print('the country code of Australia is ${au.value}');
}

Thnaks,

xsahil03x commented 4 years ago

@sinadarvi that's a pretty nice use-case. Yes, we can work on this but I think we have to create different annotations for these kinds of code generation. So that the developer won't get much confused. @astralstriker whats your views on this?

astralstriker commented 4 years ago

As good as the use case is, it is not relevant to sealed classes and therefore, super enum. This is about enum with values, which again can be emulated with super enum but really would be an overkill. I don't want to added this feature to super enum either as it doesn't glue in with the concept of sealed classes. Imo, we should make another package for this, that's maybe is simply called enum.

sinadarvi commented 4 years ago

@astralstriker sry for my poor understanding, but how can it break any concept of sealed classes? is there anything I don't see? can you explain more?

astralstriker commented 4 years ago

Might be me whose understanding is poor. As far as my understanding goes- Enum values by default have an index associated with them, starting from 0 of course, which is considered to be their values. Just as your use case suggested, you sometimes need to assign values different from indices to enum values. In your case, the enum values have the same type (String) of value assigned to them. In Kotlin and Java, this is achievable by using plain enum classes. Sealed classes come into play when there are different types of values associated with the values of an enum.

sinadarvi commented 4 years ago

@astralstriker I can understand your concern but it's not necessary to just use phrase value we can set any name suitable! and I don't think there is any conflict with sealed class concept. as you know a sealed class can have multiple subclasses and any of them can have multiple instances. so, as far as I know, shouldn't be any problem here.

@superEnum(String,"code")
enum Country{
    @value("ae")
    UnitedArabEmirates,
    @value("ar")
    Argentina,
    @value("at")
    Austria,
}

main(){
    Country country = Country.Austria;
    print(country.code)
}

and generated country.g.dart :

.
.
.
@immutable
class Argentina extends Country {
  const Argentina() : super(_Country.Argentina);

  final String code = "ar";

  @override
  String toString() => 'Argentina(code:${this.code})';

  @override
  List get props => [code];
}
.
.
.
sinadarvi commented 4 years ago

@astralstriker any idea? can we somehow figure it out?

xsahil03x commented 4 years ago

@sinadarvi thanks for the feature request but, this request is out of scope for this package and should be handled by creating another generator package.

So closing as of now :+1: