dart-lang / dart-lang/language

Allow `sealed base class`, because it is useful

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

Description

In general I think our logic for what the valid combinations are was a bit flawed because we used the table for what is allowed in _other_ libraries, but should have used a table based on what is allowed in _any_ library (including the current one). There may be other valid use cases that are not allowed, other than this one, from within the same library.

## Use case

You have a sealed class, and you want to ensure all possible implementations of that class are transitive extensions of it.

You can achieve this today by making all the subtypes in your library be themselves `base` classes, but it could be easy to miss one on accident, in particular when adding new ones. Allowing `sealed base` would allow you to enforce this property, since `base` is transitive even within the same library.

Concretely, this is coming up for the `Code` class in macros, I want every piece of `Code` to ultimately extend the base sealed class, but they must go through some more specific subtype to get there (`DeclarationCode`, `ExpressionCode`, etc).

It is OK for people to build their own subtypes of those, but only through extension. We want to be able to put in some form of validation in their constructors, and ensure that those constraints are met.

Contributor guide

Open the contributing guide

Research direction

Start by reviewing the issue's rules for valid combinations of sealed and base classes, including the distinction between the current library and other libraries. Compare those rules with the Code, DeclarationCode, and ExpressionCode use case described here. Done means the language design specifies whether sealed base classes are valid and what constraints they enforce.

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.