dart-lang / dart-lang/language

Can we allow `mixin on <SealedClass>`?

Open
#3,215 6 comments 2 reactions 0 assignees View on GitHub
request
Dominant language
TeX
Stars
2.9k
Forks
239
Avg merge
2d 18h
Merged PRs (30d)
14

Description

There might be a better way to do this, if so please let me know :).

Consider a sealed class hierarchy, with private *Impl classes, and the *Impl classes also implement another shared interface.

Internally to my code, I want to only deal in the *Impl classes, but I can't declare a type which enforces something to be both the sealed class plus the additional private interface. I end up having to choose one or the other.

### my_public_library.dart

```dart
sealed class Choice {}

abstract interface class ChoiceA implements Choice {}

abstract interface class ChoiceB implements Choice {}
```

### my_private_library.dart

```dart
import 'my_public_library.dart';

class WithId {
final int id;

WithId(this.id);
}

/// Not allowed, but **might** be safe on its own?
///
/// I believe you would only be able to actually mix this into one of the impl classes.
mixin ChoiceImpl on Choice implements WithId {}

class ChoiceAImpl extends WithId implements ChoiceA with ChoiceImpl {
ChoiceAImpl(super.id);
}

class ChoiceBImpl extends WithId implements ChoiceB with ChoiceImpl {
ChoiceBImpl(super.id);
}
```

### internal_usage.dart

```dart
import 'my_private_library.dart';

class ThingWithChoice {
/// This gives me access to both the `Choice` and `WithId` interfaces, while maintaining
/// the sealed nature of `Choice` and hiding the `WithId` iterface from users.
final ChoiceImpl choice;
}
```

Contributor guide

Open the contributing guide

Research direction

Read the examples in my_public_library.dart, my_private_library.dart, and internal_usage.dart to understand the proposed `mixin ... on Choice` use case. Done means determining whether this construct can be supported safely while preserving the sealed hierarchy and identifying the language specification work required.

Written by the indexing model from the issue text.

Assessment

Tech stack
dart
Domain
compilers
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
30/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.