dart-lang / dart-lang/language

Make maps not always use `null` as default value.

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

Description

Currently Dart maps return `null` when a lookup fails. With null safety, that means `operator[]` always returns a nullable type. That can be annoying.

Consider if `Map` had three type parameters: `K`, `V` and `D` where `D` is the type of the default value returned when a lookup fails.
It would be `Map`, and `operator[]` has return type `D`.

To avoid breaking everyone we could introduce a new `Map` type, `XMap` (probably not), and have all current `Map` instances implement `XMap`.

Then we can have `XMap` constructors taking a default value, rather than just always using `null` as the default value.
Map literals could write `{"x": 1, "y": 2, default: -1}` to create an `XMap`.

Obviously we'd want to rename `XMap` to `Map` soon enough, but without breaking code which only knows the old `Map`. That could be based on language version, and maybe type aliases.
Old code sees `typedef Map = real.Map;`, new code sees the real `Map` directly.
(Maybe they'll want a some short type aliases too, like `typedef map = Map;` )

Contributor guide

Open the contributing guide

Research direction

Start by reviewing the proposed Map, XMap, operator[] and type-alias changes in the issue. Determine whether the default-value design and language-version compatibility have an accepted direction; done requires a settled specification approach rather than an implementation patch.

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
Needs clarification
Newbie friendliness
20/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.