-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Can't use const constructors with mixins #29396
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
The main reason why these constructors cannot be const is that certain constructors are omitted when mixin application occurs. For example: In order for The superclass of For each generative constructor named $q_i(T_{i1}$ $ a_{i1}, \ldots , T_{ik_i}$ $ a_{ik_i}),
i \in 1..n$ of $S$ that is accessible to $L_M$, $C$ has an implicitly declared
constructor named $q'_i = [C/S]q_i$ of the form
$q'_i(a_{i1}, \ldots , a_{ik_i}):\SUPER(a_{i1}, \ldots , a_{ik_i});$. Decrypting the LaTeX and considering the same example, what we get is There is a problem here: The implicitly declared constructor is non-const, even in the case where its superinitializer invokes a const constructor (in which case the implicitly generated constructor could itself also have been const). So, unfortunately, there is no way to obtain a complete chain of const constructors up to This is a known problem, and the overly simple implicit generation of constructors creates some other problems as well (see #9745, #15101). The problem has been known for a long time, and it hasn't had enough urgency to be fixed so far (and the fix isn't necessarily trivial), but it is certainly on the agenda at this point and this issue helps pushing it upwards. I'll close this issue as a duplicate of #9745, and refer to this issue from there. |
This was marked as a duplicate of #9745, which is closed now, but it still doesn't seem to work. Taking the example above and marking the constructors const where the original comment says the desire is to have them be const gives:
...and:
class Superclass {
const Superclass(this.foo);
final int foo;
}
abstract class Mixin {
const factory Mixin.fixed(int baz) = _FixedMixin;
}
class _FixedMixin extends Object with Mixin {
const _FixedMixin(this.baz);
final int baz;
}
class Subclass extends Superclass with Mixin {
const Subclass(int foo, this.bar) : super(foo);
final int bar;
}
void main() {
const Subclass(0, 1);
const Mixin.fixed(0);
} I'm using |
You're looking for #32223, which is the request for implementation of this feature (this is |
The current specification for Dart 2 says that a mixin application of a mixin derived from a class with no field declarations, on top of a class with a const constructor, should make the forwarding constructor const. That might not be implemented yet, but it should be eventually. |
Right, but it is requested in #32223, so that's the place to check out the current implementation status (and also to advocate for getting it done, if the current priority assigned to this task appears to be too low). |
Consider this code:
I want the
Mixin.fixed
andSubclass
constructors to be const, but today that isn't possible. It's not clear why.The text was updated successfully, but these errors were encountered: