add method
Returns a copy of this set where value
is added to.
If value
(or an equal value) was already present in the set, the set is
returned unchanged.
Example:
var set = ImmortalSet();
final time1 = DateTime.fromMillisecondsSinceEpoch(0);
final time2 = DateTime.fromMillisecondsSinceEpoch(0);
// time1 and time2 are equal, but not identical.
expect(time1, time2);
expect(identical(time1, time2), false);
set = set.add(time1);
// A value equal to time2 exists already in the set, and the call to
// add does not expand the copy even further.
set = set.add(time2);
expect(set.length, 1);
expect(set.contains(time1), true);
expect(set.contains(time2), true);
Implementation
ImmortalSet<T> add(T value) {
final newSet = toSet();
if (newSet.add(value)) {
return ImmortalSet._internal(newSet);
}
return this;
}