Home > OS >  How do I inject an anonymous function directly into an enum
How do I inject an anonymous function directly into an enum

Time:05-16

For Dart 2.17, we can use constructors in enum similarly to how we do with classes.

I am trying to insert an anonymous function inside of an enum directly.

here is code that works but doesn't do exactly what I'd like it to do.

int _add(int a, int b) => a   b;
int _sub(int a, int b) => a - b;
int _mul(int a, int b) => a * b;
double _div(int a, int b) => a / b;

enum MyEnum {
  addition(_add),
  subtract(_sub),
  multiplication(_mul),
  division(_div);

  final Function fx;

  const MyEnum(this.fx);
}

void main() {
  var fun = MyEnum.addition;
  print(fun.fx(1, 2));
  fun = MyEnum.subtract;
  print(fun.fx(1, 2));
  fun = MyEnum.multiplication;
  print(fun.fx(1, 2));
  fun = MyEnum.division;
  print(fun.fx(1, 2));
}

Instead of making a function somewhere else in the code, as the _add, _sub, _mul, _div, I would like to directly insert an anonymous function into the enum, like in the following code (please note that the following code does not work).

What I'd like to do

enum MyEnum {
// I'd like to insert an anonymous function instead.
  addition((int a, int b) => _add(a, b)), 
  subtract((int a, int b) => a - b),
  multiplication(int a, int b) => a * b),
  division((int a, int b) => a / b;);

  final Function fx;

  const MyEnum(this.fx);
}

Is it possible? Would anyone be able to show me how to do this? I cannot figure out what I am doing wrong.

CodePudding user response:

One point of Dart 2.17 is that you no longer need to use extensions. The following code, which incorporates the features of Dart 2.17, is how I solved it. Though this certainly is not as elegant nor satisfying as I would have hoped it would have been in my original post.

enum MyEnum {
  addition(),
  subtract(),
  multiplication(),
  division();

  Function fx() {
    switch (this) {
      case addition:
        return (int a, int b) => a   b;
      case subtract:
        return (int a, int b) => a - b;
      case multiplication:
        return (int a, int b) => a * b;
      case division:
        return (int a, int b) => a / b;
      default:
        throw Exception('Unknown operation');
    }
  }

  const MyEnum();
}

void main() {
  var fun = MyEnum.addition;
  print(fun.fx()(1, 2));
  fun = MyEnum.subtract;
  print(fun.fx()(1, 2));
  fun = MyEnum.multiplication;
  print(fun.fx()(1, 2));
  fun = MyEnum.division;
  print(fun.fx()(1, 2));
}

CodePudding user response:

Have you tried extensions? I'm not sure you'll be able to do everything you want (as happened to me), but they almost got the job done. Sometimes I just have to invoke the extension instead of the enum, probably when I put some static method on the extension (like calling SexExt.staticMethod() instead of Sex.staticMethod), but I find them pretty useful.

enum Sex { MALE, FEMALE }

extension SexExt on Sex {
  String getText() {
    switch (this) {
      case Sex.MALE:
        return "Maschio";
      case Sex.FEMALE:
        return "Femmina";
    }
  }

  String getShortText() {
    switch (this) {
      case Sex.MALE:
        return "M";
      case Sex.FEMALE:
        return "F";
    }
  }
}
  • Related