[=>] is reader syntax for finite key-value association builders and association comprehensions, overloaded across map and association types.
Syntax
;; Empty association builder
[=>]
;; Itemized association builder
[⟨key-expr⟩ => ⟨value-expr⟩ ...]
;; Association comprehension
[⟨key-expr⟩ => ⟨value-expr⟩ ⟨clause⟩+]
;; ⟨clause⟩ := :for ⟨var-or-_⟩ :below ⟨bound-expr⟩
;; | :for ⟨var-or-_⟩ :in ⟨iterable-expr⟩
;; | :with ⟨var-or-_⟩ = ⟨expr⟩
;; | :when ⟨boolean-expr⟩
Semantics
[... => ...]is polymorphic over any association collection type:cwith key type:kand value type:v:- Itemized association builders
[k1 => v1 ... kn => vn](and empty[=>]) desugar to theFromItemizedAssociation :c :k :v :stypeclass protocol (begin-association-builder,adjoin-to-association-builder,finalize-association-builder), passing the exact entry countnup front. - Association comprehensions
[k => v ⟨clause⟩+]desugar to theFromAssociationComprehension :c :k :v :stypeclass protocol (begin-association-comprehension,adjoin-to-association-comprehension,finalize-association-comprehension). When the first clause is:for ⟨var⟩ :in ⟨iter⟩, the iterator’ssize-hintis passed tobegin-association-comprehension.
- Itemized association builders
- All
⟨key-expr⟩forms must unify to a single key type:k, and all⟨value-expr⟩forms must unify to a single value type:v. Entries are evaluated left-to-right in index order. - Comprehension clauses follow the same rules as
[]::for ⟨var-or-_⟩ :below ⟨bound-expr⟩iterates from0by1+while(< i bound)(for any type implementingNumandOrd).:for ⟨var-or-_⟩ :in ⟨iterable-expr⟩iterates over anyIntoIteratorsource.:with ⟨var-or-_⟩ = ⟨expr⟩introduces a local binding for subsequent clauses and the key/value expressions.:when ⟨boolean-expr⟩guards entry emission with aBooleancondition.
- Type defaulting: If the surrounding context does not fix the target type
:c, Coalton defaults:cto(Seq (Tuple :k :v))while inferring:kand:vfrom the key and value expressions. Empty builders[=>]usually requiretheto specify the map, key, and value types.
Examples
;; Itemized HashMap construction
(coalton
(the (coalton/hashmap:HashMap String Integer)
["one" => 1
"two" => 2
"three" => 3]))
;; Association comprehension building a table of squares
(coalton
(the (List (Tuple UFix UFix))
[x => (* x x) :for x :below 5 :when (odd? x)]))
; => (#.(TUPLE 1 1) #.(TUPLE 3 9))
;; Empty association builder with explicit type
(coalton
(the (coalton/hashmap:HashMap String Integer) [=>]))
Other notes
- Standard library types supporting
[=>]areHashMap,Hashtable,OrdMap,List (Tuple :k :v), andSeq (Tuple :k :v). - User-defined map or association types can opt into
[=>]syntax by implementingFromItemizedAssociationandFromAssociationComprehensionfromcoalton/classes.