jeudi 20 juillet 2017

How to get the name of a function reference?

Consider the following

Dart Code

import 'dart:math';

void main() {
  MyClass  myClass = new MyClass();
  Function pointer = ((new Random().nextInt(100) % 2) == 0 ? myClass.foo : myClass.bar);

  print('We\'re about to call function "$pointer"!\nWait for it...');
  Function.apply(pointer, []);
}

class MyClass {
  void foo() {
    print('\tjust foo\'d');
  }

  void bar() {
    print('\tjust bar\'d');
  }
}

Output

We're about to call function "Closure 'bar$0' of Instance of 'MyClass'"!

(dartpad here)

Question

How do I get this to instead print simply bar or foo? (also acceptable would be MyClass.bar or MyClass.foo)





Aucun commentaire:

Enregistrer un commentaire