Skip to content

Mixin application with no superclass generative constructors get useless default constructor. #60324

Open
@lrhn

Description

@lrhn

The front-end adds a default constructor to an anonymous mixin application class if it introduces no forwarding constructors, which only happens if the superclass has no accessible generative constructors.
In that case, the default constructors super-invocation of super() is guaranteed to be an error.

The specification is not saying anything directly about this.

The spec says which forwarding constructors a mixin application class is given, but not anything about what to do if that is zero constructors.

It says that if a class declares no constructors, it gets a default constructor.
That should probably only apply to class declarations, and not to anonymous mixin declarations, which can't declare constructors at all, and which gets forwarding constructors added instead.

It's not because mixing in a mixin on a class with no accessible generative constructors is useful. It's equivalent to implementing the mixin's interface because there is no way to extend a mixin application class with no generative constructor and make an instantiable class.

Still, it shouldn't hurt. If you have a non-base mixin declaration, you can mix it in or implement it, and you get the same interface in both cases.

If you have a base mixin declaration and you create a class that cannot have an instantiable subclass, can only be used as an interface, so if you do use it, you're inside the same library, and you could just have implemented the base mixin directly.

In any case, we should be explicit about what compilers should do in this case, and I recommend not adding a constructor that's going to be an immediate compile-time error.

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-dart-modelFor issues related to conformance to the language spec in the parser, compilers or the CLI analyzer.area-languageDart language related items (some items might be better tracked at github.com/dart-lang/language).cfe-encodingsEncoding related CFE issues.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions