Closed chharvey closed 3 years ago
This comment updates and supercedes parts of the previous comment.
Set access can be done via bracket accessor notation, where the value produced is a boolean indicating whether the element is in the set.
let s: Set.<str> = {'hello', 'world'};
s.['hello']; %== true
s.['hi']; %== false
s.[42]; %> TypeError
A TypeError is reported at compile-time if the type of the accessed element does not match the set’s invariant. In the example above, s
is of type Set.<str>
(the set’s invariant is type str
), so accessing .[42]
produces a type error since 42
is not a string. In other words, since it’s not even possible for an integer to exist in the set at all, compilation is stopped.
Optional access and claim access are irrelevant, since the returned value will always be a boolean.
s?.['hello']; %== true
s?.['hi']; %== false
s?.[42]; %> TypeError
s!.['hello']; %== true
s!.['hi']; %== false
s!.[42]; %> TypeError
When using bracket accessor notation to mutate a mutable set, setting the value true
will add the element to the set (if it’s not already added), and setting false
will remove the element (if it’s not already removed). Type errors are as usual.
let s: mutable str{} = {'hello', 'world'};
set s.['hi'] = true;
s; %== {'hello', 'world', 'hi'}
set s.['hello'] = false;
s; %== {'world', 'hi'}
set s.[42] = true; %> TypeError
set s.['world'] = anything_but_a_boolean; %> TypeError
In a way, set access can be treated like map access where the map’s keys (antecedents) are elements of the set and the map’s values (consequents) are boolean.
Sets are dynamic-sized unordered lists of values.
Sets cannot contain identical (via
===
) elements. The set{'hello', 'hello'}
only contains 1 element.Sets may have several elements that are un-identical but “equal”.
Even though
0.0 == -0.0
andx == y
, this set has four elements.Sets are considered equal (via
==
) iff they have equal elements. The order of elements in a set is not relevant.Set elements can be accessed via bracket accessor notation. If the expression in the brackets exists in the set, it is produced. (In a way, think of a Set as a Mapping whose keys and corresponding values are the same). If the compiler can determine that the value does not exist in the set, a VoidError is thrown; otherwise it’s a runtime error. An expression of an incorrect type is a TypeError.
Optional access and claim access as normal.