Collections: Interfaces

The concrete Hack collection classes are generally derived from various interfaces. Actually, many of the methods on the concrete classes take and/or return the interfaces listed here.

The general workflow will be that you will construct instances of the concrete classes, use either the concrete classes or interfaces in your parameter or return type annotations, and calls to methods on the concrete classes may likely return an interface.

Descriptions of each of the interface methods can be found in the API reference.

Core Interfaces

These are the interfaces that allow for iteration, conversion (e.g., toVector) and manipulation (e.g., filter).

Interface Description Key Methods
Traversable<T> Anything that can be iterated over using something like foreach. A key isn't necessary, just values. N/A
KeyedTraversable<Tk, Tv> Extends Traversable<T> and requires a key to exist . N/A
Container<T> Extends Traversable<T>, but only includes arrays and Hack collections N/A
KeyedContainer<Tk, Tv> Extends KeyedTraversable<Tk, Tv>, but only includes arrays and Hack collections (except for Set and ImmSet since they don't have keys) N/A
Indexish<Tk, Tv> Extends KeyedTraversable<Tk, Tv> and is anything that can be indexed into using square-bracket notation, except for objects that implement ArrayAccess. Usually has entities with keys of type string or int. It is somewhat common to have a method take an Indexish or return an Indexish. N/A
IteratorAggregate<T> Extends Traversable<T> and can produce an Iterator. Not implemented by arrays. Rarely directly used. getIterator()
Iterable<T> Extends IteratorAggerate<T> and is the primary interface that declares the methods that provides the capabilities for Hack collections. toArray(), toValuesArray(), toVector(), toImmVector(), toSet(), toImmSet(), lazy(), values(), map<Tm>((function(T): Tm) $callback), filter((function(T): bool) $callback), zip<Tz>(Traversable<Tz> $traversable), take(), takeWhile(), skip(), skipWhile(), slice(), concat(), firstValue(), lastValue()
KeyedIterable<Tk, Tv> Extends Iterable<Tv> and adds key capabilities toKeysArray(), toMap(), keys(), lazy(), take(), takeWhile(), skip(), skipWhile(), firstKey(), lastKey(), mapWithKey<Tm>(function(Tk, Tv): Tm $callback), filterWithKey(function(Tk, Tv): bool $callback), getIterator(), map<Tm>(function(T): Tm $callback), filter(function(T): bool $callback), zip<Tz>(Traversable<Tz> $traversable)

General Collection Interfaces

These interfaces persist amongst all collections, providing basic methods for querying and adding.

Interface Description Key Methods
ConstCollection<T> Read-only collection. All collections implement this interface. count(), isEmpty(), items()
OutputCollection<T> Mutable collection. All mutable collections implement this interface. add(), addAll()
Collection<T> Extends ConstCollection<T> and OutputCollection<T>. Combines the read-only and mutable collection behavior. clear()

Specific Collection Access Interfaces

All collections have some sort of access mechanism. "Getters" for immutable collections. Adding "setters" for mutable collections. These are the interfaces that exposes those access functions.

Interface Description Key Methods
ConstIndexAccess<Tk, Tv> Interface for all keyed collections to enable access to its values. at(), get(), containsKey()
IndexAccess<Tk, Tv> Extends ConstIndexAccess<Tk, Tv> and enables mutable collections for setting keys. set(), setAll(), removeKey()
ConstSetAccess<Tm> Interface for all sets to enable access to its values. contains()
SetAccess<Tm> Extends ConstSetAccess<Tm> and enables mutable sets for removing values. remove()
ConstMapAccess<Tk, Tv> Extends ConstSetAccess<Tk> and ConstIndexAccess<Tk, Tv> and enable access to a map's values. N/A
MapAccess<Tk, Tv> Extends ConstMapAccess<Tk, Tv>, SetAccess<Tk> and IndexAccess<Tk, Tv> and enables mutable maps for setting and removing keys and values. N\A

Specific Collection Interfaces

Each concrete Hack collection class implements the appropriate interface listed here associated with that class.

Interface Description Key Methods
ConstSet<Tv> Extends ConstCollection<Tv>, ConstSetAccess<Tv>, Container<Tv> and KeyedIterable<mixed, Tv> and represents a read-only set. A bunch.
MutableSet<Tv> Extends ConstSet<Tv>, SetAccess and Collection<Tv> and represents a mutable set. A bunch.
ConstVector<Tv> Extends ConstCollection<Tv>, ConstIndexAccess<int, Tv>, Indexish<int, Tv> and KeyedIterable<int, Tv> and represents a read-only vector (sequence of values). A bunch.
MutableVector<Tv> Extends ConstVector<Tv>, IndexAccess<int, Tv> and Collection<Tv> and represents a mutable vector. A bunch.
ConstMap<Tk, Tv> Extends ConstCollection<Pair<Tk, Tv>>, ConstMapAccess<Tk, Tv>, Indexish<Tk, Tv> and KeyedIterable<Tk, Tv> and represents a read-only mapping of keys Tk to values Tv. A bunch.
MutableMap<Tk, Tv> Extends ConstMap<Tk, Tv>, MapAccess<Tk, Tv> and Collection<Pair<Tk, Tv>> and represents a mutable map. A bunch.