dart-lang / dart-lang/language

Permits clause for sealed types

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

Description

I've been toying around with sealed types and in my opinion they aren't really pleasant to use.

Sealed types require the entire hierarchy to be in the same library. This is fine when there are only a few subclasses or if they are relatively short. However, anything beyond that requires either the entire hierarchy to be in the same file, giving rise to 1k+ lines monstrosities, or split into several files using `part '...;'` and `part of'...'` directives. Neither of which are really pleasant. The latter also runs contrary to the advice in https://dart.dev/guides/libraries/create-library-packages#organizing-a-library-package which discourages the use of `part '...'` and `part of '...'`.

A real life example where I encountered this was when I was migrating part of my `Range` library, https://github.com/forus-labs/cauldron/tree/master/sugar/lib/src/core/range. Each subclass really isn't trivial.

I think it'll be really great if a `permits` clause was introduced, similar to that proposed in https://openjdk.org/jeps/360.

For example:

Range.dart:
```dart
import 'package:my_package/all.dart'

sealed class Range permits Interval, Singleton {}
```

Interval.dart:
```dart
final class Interval extends Range {}
```

Singleton.dart:
```dart
final class Singleton extends Range {}
```

Contributor guide

Open the contributing guide

Research direction

Start with the proposed Range.dart, Interval.dart, and Singleton.dart examples, then read the linked Dart library-organization guidance and JEP 360. Define the permits syntax and its sealed-type semantics before identifying the relevant Dart language specification and implementation entry points. Done means the proposal has an agreed design and the corresponding language changes are covered.

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.