unwrap-as is an expression macro that performs a fallible TryInto conversion and immediately unwraps the resulting Optional, panicking if conversion fails.
Syntax
;; Convert `⟨expr⟩` to `⟨type⟩` via `TryInto` and unwrap
(unwrap-as ⟨type⟩ ⟨expr⟩)
;; Produce a unary unwrapping conversion function of type `(:a -> ⟨type⟩)`
(unwrap-as ⟨type⟩)
Semantics
(unwrap-as ⟨type⟩ ⟨expr⟩)expands to(the ⟨type⟩ (coalton/classes:unwrap (coalton/classes:tryInto ⟨expr⟩))).(unwrap-as ⟨type⟩)with one argument expands to(fn (x) (the ⟨type⟩ (coalton/classes:unwrap (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). - Returns the converted value of type
⟨type⟩when(tryInto ⟨expr⟩)produces(Some v), or signals aPanicerror if it producesNone. ⟨type⟩may reference scoped type variables bound by an enclosingforall.
Examples
;; Narrow an Integer known to fit in U16
(coalton (unwrap-as U16 (the Integer 1024)))
; => 1024
;; Use the one-argument form `(unwrap-as ⟨type⟩)` in a pipeline
(coalton
(map (unwrap-as U8) (the (List Integer) [10 20 30])))
; => (10 20 30)