google / google/quiver-dart

Consider making `Optional` not allocate a new absent per type.

Open
#397 0 comments 0 reactions 0 assignees View on GitHub
cleanup enhancement
Dominant language
Dart
Stars
1.1k
Forks
133
PR merge metrics
No merged PRs in 30d

Description

I see code like `Optional something = const Optional.absent();`. This allocates a new empty optional per type, even though they all behave the same.

Have you considered using a single `Optional` for all absent optionals?

Even if you can't change the API at this point, you could still make the above allocate an `Optional` instead by writing the constructor as:
```dart
const factory Optional.absent() = Optional._absent;
const Optional._absent() : _value = null;
```
(A little hack-ish, but it should work :).

Since `Optional` is a subtype of any other `Optional`, you get to reuse the same const instance in all the places it's created without type issues.

Contributor guide

Open the contributing guide

Research direction

Start by locating the Optional implementation and its Optional.absent entry point, then inspect how absent values are constructed and tested. The change is complete when absent optionals reuse a single const instance across types without breaking the existing API, and the relevant tests confirm the behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
dart
Domain
tooling
Issue type
Refactor
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.