Chapter 14. The Swarm Collections Library

Table of Contents
14.1. Overview: the List, Map and Array Protocols
14.2. Choosing between Lists, Maps, and Arrays
14.3. Using Swarm Arrays
14.4. Swarm Maps
14.5. Accessing Collections with Indices

14.1. Overview: the List, Map and Array Protocols

The most frequently used kinds of collections are the List, Map and Array protocols. They have some elements in common. They all comply with the Collection protocol, most importantly, which means they have methods with which items can be added, retrieved, and removed. Also, each can be used to create an index object, which can make management of lists an easier chore.

It is very important to note that these collections are primarily intended to manage objects, not integers or floating point values. [1] If you need an array of integers (or floats or chars or whatever else), just use an ordinary C array. When it is necessary to use strings, integers, or floating point values in a Swarm collection, there are two workarounds. One is to use typecasting to put those other values inside the space allocated for a pointer to an object. For various reasons, that approach is not as desirable as the alternative of creating "wrapper objects" that can contain those other types of variables. In short, while typecasting will often work, it is generally a better strategy is to design more carefully the objects you want to keep in Swarm collections and use recommended procedures for retrieving them.

Some commands that work in Swarm collections are:

This chapter does not discuss the Swarm Set protocol because, at the current time, it has no functionality beyond the regular Swarm List protocol.



Type casting for both storage of variables in collections as well as usage of non-object values for keys was discussed in the original Swarm design. Roger Burkhart defined a protocol MemberType which would have been adopted by the Collection protocol: "The MemberType option may be used to declare the type of member which a collection contains. Its value must be an object having one of the ValueType types defined in defobj. (..Currently no ValueType objects are implemented, so MemberType is not supported.)" This protocol was to have two methods: -setMemberType: aDataType and -getMemberType.