<- is the monadic bind operator inside do notation, binding a pattern to the value produced by a monadic action.
Syntax
(do
...
(⟨pattern⟩ <- ⟨monadic-expr⟩)
...
⟨final-expr⟩)
Semantics
(⟨pattern⟩ <- ⟨monadic-expr⟩)is valid only as a non-final clause of adoexpression; adoblock cannot end with a<-clause.⟨monadic-expr⟩must have type(:m :a), where:mis theMonadtype constructor of the enclosingdoblock, and⟨pattern⟩must have type:a.⟨pattern⟩may be a variable,_, a constructor pattern (such as(Tuple x y)), or an=alias pattern.- The clause desugars to a call to
coalton/classes:>>=:(>>= ⟨monadic-expr⟩ (fn (G) (match G (⟨pattern⟩ ⟨remaining-do-body⟩)))) - Variables bound by
⟨pattern⟩are in scope for all subsequent clauses and⟨final-expr⟩in thedoblock. returnis not permitted inside⟨monadic-expr⟩.
Examples
;; Binding variables and destructuring a Tuple inside `do`
(coalton
(do
(x <- (Some (the Integer 10)))
((Tuple y z) <- (Some (Tuple 20 12)))
(pure (+ x (+ y z)))))
; => 42