mixin_class_declaration_extends_not_object 진단: mixin class가 Object 외의 클래스를 extends 하면 안 돼요

mixin_class_declaration_extends_not_object 진단: mixin class가 Object 외의 클래스를 extends 하면 안 돼요

분석기가 내보내는 진단에는 이름이 있어요. mixin_class_declaration_extends_not_objectmixin 한정자로 표시된 클래스가 Object가 아닌 다른 클래스를 extends 할 때 Dart 분석기가 알려주는 컴파일 타임 진단이에요. mixin class는 Object가 아닌 슈퍼클래스를 가질 수 없어요.

출처: mixin_class_declaration_extends_not_object

본문

설명

분석기는 mixin 한정자가 붙은 클래스가 Object가 아닌 다른 클래스를 extends 하면 이 진단을 만들어요. mixin class 선언은 Object가 아닌 슈퍼클래스를 가질 수 없기 때문이에요.

예시

다음 코드는 mixin 한정자를 가진 클래스 BA를 extends 하기 때문에 이 진단이 나와요.

class A {}

mixin class B extends A {}

흔한 해결 방법

클래스를 mixin으로 사용하고 싶다면, 슈퍼클래스를 Object로 바꾸거나 extends 절을 아예 제거해 줘요.

class A {}

mixin class B {}

클래스에 Object가 아닌 슈퍼클래스가 필요하다면, mixin 한정자를 제거해 줘요.

class A {}

class B extends A {}

mixin과 Object가 아닌 클래스의 서브클래스, 둘 다 필요하다면, 서브클래스의 멤버들을 새 mixin으로 옮기고 서브클래스에서 mixin 한정자를 제거한 뒤 그 새 mixin을 서브클래스에 적용해 줘요.

class A {}

class B extends A with M {}

mixin M {}

서브클래스의 멤버에 따라서는 mixin에 on 절을 추가해야 할 수도 있어요.

더 알아보기