Incompatible parameters in redirected constructorsDRT-W1589
The analyzer produces this diagnostic when a factory constructor attempts to redirect to another constructor, but the two have incompatible parameters. The parameters are compatible if all of the parameters of the redirecting constructor can be passed to the other constructor and if the other constructor doesn't require any parameters that aren't declared by the redirecting constructor.
Examples
The following code produces this diagnostic because the constructor for A
doesn't declare a parameter that the constructor for B
requires:
abstract class A {
factory A() = B;
}
class B implements A {
B(int x);
B.zero();
}
The following code produces this diagnostic because the constructor for A
declares a named parameter (y
) that the constructor for B
doesn't
allow:
abstract class A {
factory A(int x, {int y}) = B;
}
class B implements A {
B(int x);
}
Common fixes
If there's a different constructor that is compatible with the redirecting constructor, then redirect to that constructor:
abstract class A {
factory A() = B.zero;
}
class B implements A {
B(int x);
B.zero();
}
Otherwise, update the redirecting constructor to be compatible:
abstract class A {
factory A(int x) = B;
}
class B implements A {
B(int x);
}