What is a typedef in Dart?

后端 未结 5 1602
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-04 15:34

I have read the description, and I understand that it is a function-type alias.

  • A typedef, or function-type alias, gives a function type a name that you can

5条回答
  •  情歌与酒
    2020-12-04 15:59

    Just slightly modified answer, according to the latest typedef syntax, The example could be updated to:

    typedef LoggerOutputFunction = void Function(String msg);
    
    class Logger {
      LoggerOutputFunction out;
      Logger() {
        out = print;
      }
      void log(String msg) {
        out(msg);
      }
    }
    
    void timestampLoggerOutputFunction(String msg) {
      String timeStamp = new Date.now().toString();
      print('${timeStamp}: $msg');
    }
    
    void main() {
      Logger l = new Logger();
      l.log('Hello World');
      l.out = timestampLoggerOutputFunction;
      l.log('Hello World');
    }
    

提交回复
热议问题