try-as is an expression macro for fallible type conversions via the TryInto type class, returning an Optional.
Syntax
;; Attempt to convert `⟨expr⟩` to `⟨type⟩`, returning `(Optional ⟨type⟩)`
(try-as ⟨type⟩ ⟨expr⟩)
;; Produce a unary fallible conversion function of type `(:a -> (Optional ⟨type⟩))`
(try-as ⟨type⟩)
Semantics
(try-as ⟨type⟩ ⟨expr⟩)expands to(the (Optional ⟨type⟩) (coalton/classes:tryInto ⟨expr⟩)).(try-as ⟨type⟩)with one argument expands to(fn (x) (the (Optional ⟨type⟩) (coalton/classes:tryInto x))).- Requires an instance of
TryInto :src ⟨type⟩, where:srcis the inferred type of⟨expr⟩(or the input type of the returned unary function). - Evaluates to
(Some value)when⟨expr⟩can be converted to⟨type⟩, orNonewhen the conversion fails (such as an out-of-range integer conversion). ⟨type⟩may reference scoped type variables bound by an enclosingforall.
Examples
;; Bounds-checked conversion from Integer to U8
(coalton
(Tuple (try-as U8 (the Integer 200))
(try-as U8 (the Integer 300))))
; => #.(TUPLE 200 #.NONE)
;; Use the one-argument form `(try-as ⟨type⟩)` with `map`
(coalton
(map (try-as U8) (the (List Integer) [-1 0 127 255 256])))
; => (#.NONE 0 127 255 #.NONE)