On this page:
Listable
Listable.to_  list
Listable.expect_  of
Listable.to_  list
8.17.0.1

9.3 Listables🔗ℹ

A listable value can be converted to a list using a to_list operation such as Listable.to_list. Lists are themselves listable, where List.to_list just returns the same list. Mutable lists, pair lists, arrays, listable ranges, and instances of classes that implement Listable are also listable.

List-splicing contexts generally allow listables, including in the construction of a list. As a result, if expr produces a listable, another way to convert it to a list is [& expr], while PairList[& expr] would convert it to a pair list.

Typically, a listable object is also sequence and directly indexable, but listable does not imply indexable or working as a sequence.

An interface that a class can implement (publicly or privately) to make instances of the class convertable to a list. As an annotation, Listable matches all listable objects, not just instances of classes that publicly implement the Listable interface.

The interface has a single abstract method:

class Posn(x, y):

  private implements Listable

  private override method to_list():

    [x, y]

> def l :: Listable = Posn(1, 2)

> l.to_list()

[1, 2]

Converts any listable value to a list.

> Listable.to_list([1, 2, 3])

[1, 2, 3]

> Listable.to_list(PairList[1, 2, 3])

[1, 2, 3]

> Listable.to_list(Array(1, 2, 3))

[1, 2, 3]

annotation

Listable.expect_of(ann)

An annotation like Listable, with static information indicating that elements have the static information of ann. The extracted elements are not checked or converted, however, and ann is used only for its static information.

A converter annotation that is satisfied by any listable value and converts it to a List.

> def sizes :: Listable.to_list = Array("small", "medium", "large")

> sizes

["small", "medium", "large"]

fun avg(ns :: (Listable.to_list && NonemptyList.of(Number))):

  math.sum(& ns) / ns.length()

> avg([1, 2, 3])

2

> avg(Array(10, 20, 30, 40))

25

> avg(Array("small", "medium", "large"))

avg: argument does not satisfy annotation

  argument: Array("small", "medium", "large")

  annotation: (Listable.to_list && NonemptyList.of(Number))