filip
June 10, 2025, 12:46pm
1
Today, I realized that a Dart feature I didn’t know I wanted is to be able to define local enums.
I wrote this down as a feature request for the Dart team:
opened 12:26PM - 10 Jun 25 UTC
feature
I'd love to be able to define local enums the same way I can define local functi… ons or variables. Like so:
```dart
class Blah {
// ... a lot of code ...
Result process(Input input, bool significant) {
// This enum is only useful in this method. I don't need to put it all the way to the outside of the class.
enum Semaphore { red, orange, green }
final Semaphore type;
if (input.signal >= 3.5) {
type = Semaphore.red;
} else if (input.mood) {
type = Semaphore.orange;
} else {
type = Semaphore.green;
}
// Use exhaustive switch statements etc.
}
// ... a lot of code (rest of class) ...
}
```
I think enums are great. They are able to make intents much clearer, and with exhaustive switch statements, they guard against several types of bugs (forgotten cases). I also like to extract an enum first (like above) and then use its value several times (e.g. having two separate switch statements, one using the value alone, another in addition to another value).
Inside local function scope, we can of course use `bool` (a kind of enum, if you think about it) and also any enums that are defined outside the class, but you can't just create a local enum. If you have something more than a binary `true`/`false` for decision, you have to either:
1. Forget about exhaustiveness checking and do things like `else if (foo == 3)`
2. Try to use several boolean values (e.g. `if (bald && canSing)`) -- but that's not always possible or desirable
3. Create a new enum outside the class
The last option adds friction. The new enum, however small, now sits many pages of code away. So it's not right there for the programmer to see. It also pollutes the scope for the whole library despite being a one-off. So now you have some kind of `_SemaphoreProcessingType` at the end of the file, and just by reading it, you don't know where it's used.
I’d be grateful for feedback here or on the github thread.
5 Likes
This is a great feature.
This discussion might be relevant since enums are basically a special kind of class in dart
opened 07:34PM - 29 Apr 19 UTC
feature
Allow declaring a class in the body of another class.
```dart
class Person … {
class Occupation {
String jobTitle;
Occupation(this.jobTitle);
String named(String name) =>
moniker(Person(name, this));
}
String name;
Occupation occupation;
Person(this.name, this.occupation);
static String moniker(Person p) =>
'${p.name} The ${p.occupation.jobTitle}';
}
Person p = Person("Rosie", Person.Occupation("Riveter"));
```
I'd expect that `Occupation` is not an instance variable of `Person`, if that would even be a sensible notion in Dart, instead the outer class acts as a namespace containing the inner class. The inner class cannot be accessed from a `Person` instance. The inner class cannot capture instance variables from the outer class. It can howewer access static methods and fields.
Nested classes could be used for:
* Indicating a relationship between classes / encapsulating classes which only serve to complement another (e.g. Flutter `StatefulWidget`s and their respective `State`s)
* Creating arbitrary namespaces like `export _ as _` but without a library declaration file
* Encapsulating many related methods under a namespace
1 Like
Since you are there (and since we still have inline functions available), what about inline classes? I used to love those in C#.
final class SomePublicClass {
const SomePublicClass();
...
final class _SomePrivateClass() {
}
}