Skip to content

Instantly share code, notes, and snippets.

@gnprice
Last active February 4, 2023 06:39
Show Gist options
  • Save gnprice/55a5691a6d2ae00eaf87a76b8f6a3fd1 to your computer and use it in GitHub Desktop.
Save gnprice/55a5691a6d2ae00eaf87a76b8f6a3fd1 to your computer and use it in GitHub Desktop.
Exhaustiveness failure on Dart map patterns
import 'dart:collection';
void main() {
print(test({})); // -> unknown
print(test(BadMap2(false, 5))); // -> double
print(test(BadMap())); // -> oops! this map must be a sneaky one
}
String test(Map<bool, int> map) => switch (map) {
{} => 'unknown',
{false: _} => 'false',
{true: _} => 'true',
{false: _, true: _} => 'contradiction',
{false: _, false: _} => 'double',
_ => 'oops! this map must be a sneaky one',
};
class BadMap<K, V> extends UnmodifiableMapBase<K, V> {
@override
int get length => 1;
@override
List<K> get keys => [];
@override
V? operator[](Object? key) => null;
}
class BadMap2<K, V> extends UnmodifiableMapBase<K, V> {
BadMap2(this._key, this._value);
K _key;
V _value;
@override
int get length => 2;
@override
List<K> get keys => [_key];
@override
V? operator[](Object? key) => key == _key ? _value : null;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment