1

I run the code below and I got an error without any stack trace.

My code:

typedef Check<T>(T value, [onError(T value)]);

main () {
  List<Check> checks = [
    (str) => str != null,
    (str) => !str.isEmpty
  ];

  Check<String> doCheck = (String value, [onError(String)]) {
    checks.forEach((Check check) {
      if (?onError) {
        check(value, onError);
      } else {
        check(value);
      }
    });
  };

  doCheck("10");
}

And, the error I got.

file:///..()../sample.dart': Error: line 11 pos 12: formal parameter name expected
if (?onError) {

I want to get onError as an optional parameter in doCheck function, and pass this parameter to other functions in checks.

I confirmed to forward an optional parameter to 'one' function...

Is this one of restrictions to optional parameters?

4

1 に答える 1

2

バグだと思います(8007号を参照)。これを回避するには、一時変数を使用する必要があります。

typedef Check<T>(T value, [onError(T value)]);

main () {
  List<Check> checks = [
    (str) => str != null,
    (str) => !str.isEmpty
  ];

  Check<String> doCheck = (String value, [onError(String)]) {
    final isOnErrorPresent = ?onError;
    checks.forEach((Check check) {
      if (isOnErrorPresent) {
        check(value, onError);
      } else {
        check(value);
      }
    });
  };

  doCheck("10");
}
于 2013-01-20T14:19:18.573 に答える