implicit_call_tearoffs: 객체를 Function으로 쓸 때는 `call` 메서드를 명시적으로 tear-off하세요

implicit_call_tearoffs: 객체를 Function으로 쓸 때는 call 메서드를 명시적으로 tear-off하세요

implicit_call_tearoffs 린트는 객체를 Function 타입으로 쓸 때 암묵적인(implicit) call tear-off 대신 명시적으로 .call을 tear-off하도록 권장하는 규칙이에요. 명시적인 tear-off가 더 명확하고, 미래 버전에서 암묵적 동작이 사라질 가능성도 있어요. Dart 2.19에서 도입됐어요.

출처: implicit_call_tearoffs

본문

설명

DO — 객체를 Function 타입에 할당할 때는 .call 메서드를 명시적으로 tear-off해요. 명시적인 tear-off는 마법 같은 동작이 덜해요. 미래의 언어 버전은 암묵적인 call tear-off를 제거할 수도 있어요.

나쁜 예:

class Callable {
  void call() {}
}
void callIt(void Function() f) {
  f();
}

callIt(Callable());

좋은 예:

class Callable {
  void call() {}
}
void callIt(void Function() f) {
  f();
}

callIt(Callable().call);

활성화(Enable)

implicit_call_tearoffs 규칙을 활성화하려면 analysis_options.yaml 파일의 linter > rules 아래에 implicit_call_tearoffs를 추가해요.

linter:
  rules:
    - implicit_call_tearoffs

대신 YAML 맵 문법으로 린터 규칙을 설정한다면, linter > rules 아래에 implicit_call_tearoffs: true를 추가해요.

linter:
  rules:
    implicit_call_tearoffs: true

더 알아보기