On this page:
+  +
Appendable
8.12

6.40 Appendables🔗ℹ

An appendable value is one that supports ++. Maps, lists, arrays, sets, strings, and byte strings are all appendable, as are instances of classes that implement Appendable.

operator

operator ((v1 :: Map) ++ (v2 :: Map)) :: Map

 

operator

operator ((v1 :: Set) ++ (v2 :: Set)) :: Set

 

operator

operator ((v1 :: List) ++ (v2 :: List)) :: List

 

operator

operator ((v1 :: PairList) ++ (v2 :: PairList)) :: PairList

 

operator

operator ((v1 :: Array) ++ (v2 :: Array)) :: MutableArray

 

operator

operator ((v1 :: ReadableString) ++ (v2 :: ReadableString))

  :: String

 

operator

operator ((v1 :: Bytes) ++ (v2 :: Bytes)) :: MutableBytes

 

operator

operator ((v1 :: Appendable) ++ (v2 :: Appendable)) :: Any

Appends v1 and v2 to create a new map, set, list, array, string, or byte string, or calls the append method for an Appendable instance.

In the case of maps, mappings for keys in v2 replace ones that exist already in v1. In the case of sets, the new set has all of the elements of v1 and v2. In the case of lists, pair lists, strings, and byte strings, the elements of v1 appear first in the result followed by the elements of v2.

The combination map_expr ++ {key_expr: val_expr} is recognized by the compiler and turned into an efficient functional update of the map produced by map_expr, as opposed to creating an intermediate map. Set update is handled similarly.

When v1 is an instance of a class that implements Appendable, then v2 must be an instance of the same class or a subclass that inherits the same append method.

The use_static declaration constrains ++ to work only when the left-hand argument has static information indicating that it satisfies Appendable.

> {1, 2, 3} ++ {"four", "five"}

{1, 2, 3, "five", "four"}

> [1, 2, 3] ++ [4, 5]

[1, 2, 3, 4, 5]

> "hello" ++ " " ++ "world"

"hello world"

> def m = {"x": 1, "y": 2}

> m ++ {"x": 0}

{"x": 0, "y": 2}

> m

{"x": 1, "y": 2}

Provided only in the class space and annot space, not the namespace space.

An interface that a class can implement (publicly or privately) to make instances of the class work with ++. As an annotation, Appendable matches all appendable objects, not just instances of classes that publicly implement the Appendable interface.

The interface has a single abstract method:

There is no requirement on the result of the append method, but by convention, the result is the same class as the arguments.

class Posn(x, y):

  private implements Appendable

  private override method append(other :: Posn):

    Posn(x + other.x, y + other.y)

> Posn(1, 2) ++ Posn(200, 100)

Posn(201, 102)