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

Create a DataField with specific type of generic type #30

Closed flappyBug closed 4 years ago

flappyBug commented 4 years ago

I have trouble using a specific type of generic type in DataField. Let's take List<int> for example.

@superEnum
class _Event {
    @Data(fields: [DataField('foo', List<int>)])
    Foo,
}

The above code won't compile, with following error message:

  error • In constant expressions, operands of this operator must be of type 'num' •
  error • The operator '<' isn't defined for the class 'Type' •
  error • A comparison expression can't be an operand of another comparison expression •
  error • Arguments of a constant creation must be constant expressions •
...

Seems like dart compiler takes <> as two separate part. With some hard trying, I changed the code to

@superEnum
enum _Event {
  @Data(fields: [DataField('foo', <int>[].runtimeType)])
  Foo,
}

That was much better, with the following error message though:

  error • Arguments of a constant creation must be constant expressions •
  error • The values in a const list literal must be constants •

BTW, the following code do compile without using super_enum:

class Data {
  final List<DataField> fields;

  const Data({this.fields});
}

class DataField {
  final String name;
  final Type type;

  const DataField(this.name, this.type);
}

void main() {
  print(Data(fields: [DataField('scanResult', <String>[].runtimeType)]));
}
jarekb123 commented 4 years ago

Unfortunately you cannot use non-const values in annotations.

const DataField('scanResult', <String>[].runtimeType); # throws an error
astralstriker commented 4 years ago

We're working on adding support for this. The update should be out by tomorrow.

SandroMaglione commented 4 years ago

Is it possible to add the same functionality also for BuiltList<Foo>?

@superEnum
class _Event {
    @Data(fields: [
        DataField('foo', BuiltList<int>)
    ])
    Foo,
}
xsahil03x commented 4 years ago

Added in #33