dart-lang / sdk

The Dart SDK, including the VM, JS and Wasm compilers, analysis, core libraries, and more.
https://dart.dev
BSD 3-Clause "New" or "Revised" License
10.08k stars 1.56k forks source link

Cascading and callable #56586

Closed Solido closed 2 weeks ago

Solido commented 2 weeks ago

Callable is not compatible with cascading.

In my case it is used as an init() lifecycle but I want to keep the reference of the A instance.

If force 2 lines declaration and it's surprising to have to make a direct reference to call.

Thanks!

void main() {
  A()..call('OK');
  A()..('Fail');
}

class A {

  call(String s) {
    ...
  }
}
dart-github-bot commented 2 weeks ago

Summary: The Dart analyzer and linter flag an error when using the call method with cascading syntax. This is because the call method is not compatible with cascading, which leads to unexpected behavior and forces users to write code in a less concise way.

lrhn commented 2 weeks ago

Correct, you have to use .call. Same for o?.call(...).

The latter has advantages over o?(...) in the it doesn't conflict with the conditional operator so easily.

There are no current plans to allow (...) after a .. or ?.. Definitely not after ?., and probably not after just one of them either.

Solido commented 2 weeks ago

It saddens me but they're reasons and these are the specs. Thanks!