Skip to content

Allow mixins as superclasses. Deprecate using classes as mixins. #1529

Open
@lrhn

Description

@lrhn

We currently allow some classes to be used as mixins. We always intended to stop doing that, but it's proven hard to make people migrate away from existing mixin-classes because someone might be extending the class as well, and you can't extends Mixin with something declared by a mixin Mixin declaration.

So, to move things along, we should:

  • Allow a mixin declaration as a superclass, writing class C extends Mixin ... or class C = Mixin with .... It effectively means using Object with Mixin as the superclass. (A desugaring would be that each mixin Mixin ... declaration gets a synthetic class Mixin$class = Object with Mixin; declaration next to it (fresh, unforgeable name), and all uses of Mixin as superclass instead refers to Mixin$class).
  • Immediately deprecate writing extends Mixin [ with ...] and recommend it being written as with Mixin [ , ...] instead. Deprecate class C = Mixin with ... and recommend class C = Object with Mixin, ...; instead.
  • Deprecate using a class as a mixin (any mixin application of a class declaration), to encourage authors to migrate their mixin classes to mixin declarations.

This should allow changing all existing mixin-classes to mixin declarations without breaking anyone, and it will encourage people to do so by giving deprecation warnings for any use of a class as a mixin, and any use of a mixin as a class.
The recommended approach is to use mixin for anything which can be mixed in, and only use those with with, not extends.

In a later version of the language, we will then remove the ability to use mixins as superclasses again, along with the ability to use classes as mixins. That will likely not be before Dart 3.0.

Metadata

Metadata

Assignees

No one assigned

    Labels

    featureProposed language feature that solves one or more problems

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions