empty_collections 1.0.0

  • README.md
  • CHANGELOG.md
  • Installing
  • Versions
  • 8

empty_collections

The package empty_collections contains const implementations for several collection classes that allow access to there remove-functionality:

const EmptyIterator, const EmptyIterable, const EmptySet, const EmptyMap, const EmptyList, const EmptyLinkedList and const EmptyQueue.

All these classes have been implemented as permanently empty collection classes with a const constructor. This makes these classes highly efficient and e.g. usable as default arguments for optional parameters. The difference from other unmodifiable implementations (e.g. const [], const {}, const UnmodifiableSetView.empty() from the collection package) is that these classes don't adhere to the unmodifiable convention that disallows both: adding methodes (like add(...), insert(...), putIfAbsent(...), []=, ...) and removing methodes (like remove(...), clear(), retainWhere(...), ...). The thing is for empty collections the normal remove semantic doesn't change the state of the collection! E.g. a call to emptySet.clear() does nothing on an empty set, a call to emptyList.remove(o) will just return false, because o wasn't an element of the empty list to start with.

So there is no harm in allowing remove-functionality while the adding-functionlity is still of limits.

Usage

A simple usage example:

import 'package:empty_collections/empty_collections.dart';

main() {
  // use empty collection as default values
  var args = const EmptyList();
  if(userProvidesArgs()) {
    args = getUserProvidedArgs();
  }
  // remove-functionality doesn't throw exceptions!
  // in this specific case remove returns false
  // because "stupid option" wasn't an element of the empty list to start with
  args.remove("stupid option");
  
  // if you want to make sure you can add elements at some point, use toList(growable: true)
   args = args.toList(growable: true);
   args.add("important option");
   ...
}

// use empty collections as default values for optional parameters
void someMethodeWithOptionParameter([Map<String, String> environmentVariables = const EmptyMap, bool ignoreEnv = true]) {
  if(ignoreEnv) {
    // empty collections can be cleared without problems (because they are already empty to start with)
    environmentVariables.clear();
  }
  ...
}

Features and bugs

Please file feature requests and bugs at the issue tracker.

Changelog

1.0.0

  • Initial version supports const contructors for EmptyIterator, EmptyIterable, EmptySet, EmptyMap, EmptyList, EmptyLinkedList and EmptyQueue

1. Depend on it

Add this to your package's pubspec.yaml file:


dependencies:
  empty_collections: "^1.0.0"

2. Install it

You can install packages from the command line:

with pub:


$ pub get

with Flutter:


$ flutter packages get

Alternatively, your editor might support pub get or packages get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:


import 'package:empty_collections/empty_collections.dart';
        
Version Uploaded Documentation Archive
1.0.0 May 26, 2016 Go to the documentation of empty_collections 1.0.0 Download empty_collections 1.0.0 archive

Analysis

This feature is new.
We welcome feedback.
More details: scoring.

We analyzed this package, and provided a score, details, and suggestions below.

  • tool failures on Feb 3, 2018
  • Dart: 2.0.0-dev.20.0
  • pana: 0.10.1

Scores

Popularity:
Describes how popular the package is relative to other packages. [more]
0 / 100
Health:
Code health derived from static analysis. [more]
6 / 100
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
29 / 100
Overall score:
Weighted score of the above. [more]
8

Platforms

Detected platforms: Flutter, web, other

No platform restriction found in primary library package:empty_collections/empty_collections.dart.

Suggestions

  • Fix lib/src/empty_collections_base.dart.

    Strong-mode analysis of lib/src/empty_collections_base.dart failed with the following error:

    line: 84 col: 3
    Invalid override. The type of 'EmptyIterable.expand' ('((E) → Iterable<dynamic>) → Iterable<dynamic>') isn't a subtype of 'Iterable<E>.expand' ('<T>((E) → Iterable<T>) → Iterable<T>').

  • Fix issues reported by dartanalyzer.

    dartanalyzer reported 1 error(s) and 0 warning(s).

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=1.12.0 <2.0.0
Dev dependencies
test >=0.12.0 <0.13.0