dart-lang / dart-lang/language

Composing `sealed` classes

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

Description

Is there currently any way to compose sealed classes in Dart? I would like to have the ability to construct a sealed class hierarchy using another already declared sealed hierarchy.

For example, consider the following code snippet:
```dart
sealed class A {}

class A1 extends A {}

class A2 extends A {}

// A can be instantiated with A1 or A2
// A a = A1 | A2

sealed class B keyword A {} // keyword or another syntax

class B1 extends B {}

class B2 extends B {}

// B = A | B1 | B2
// which should expand to:
// B = A1 | A2 | B1 | B2

void main() {
A a = A1(); // Fine
A a2 = B1(); // Error
B b = A1(); // Fine
B b2 = B1(); // Fine
}
```

I don't want to change the `sealed class A` hierarchy.

Contributor guide

Open the contributing guide

Research direction

The issue contains a language-design proposal but names no implementation file, test, or entry point. Start by reviewing the sealed-class example and the existing Dart rules for class hierarchies; done would require an accepted syntax and specified semantics for composing A with B1 and B2 without changing A.

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
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.