flan/test/programs/when-value.flan

43 lines
1.3 KiB
Plaintext

;;;; A when whose value is kept answers an Option: Some of its body when the
;;;; test holds, None when it does not. As a statement it answers nothing.
;;;; Where a dyn is wanted it answers the body or nil, since dyn has no
;;;; Option. A body that is already an Option is not flattened.
(defn show [o (Option i32)] ()
(match o (Some v) (println v) None (println "none")))
;; Returned: the return type is the want.
(defn half [n i32] (Option i32) (when (= 0 (% n 2)) (/ n 2)))
;; Nested, as Rust's bool::then: None from the body stays apart from a
;; failed test.
(defn wrap [c bool o (Option i32)] (Option (Option i32)) (when c o))
(defn level [oo (Option (Option i32))] ()
(match oo
(Some o) (match o (Some v) (println v) None (println "some none"))
None (println "none")))
;; Dyn: the body or nil.
(defn dyn-when [x] dyn (when x 5))
(defn main [] ()
(show (half 10))
(show (half 7))
;; A let's value.
(let [a (when (> 3 2) 42)
b (when (> 2 3) 42)]
(show a)
(show b))
;; An argument.
(show (when true 9))
(level (wrap true (Some 1)))
(level (wrap true None))
(level (wrap false (Some 1)))
(println (dyn-when true))
(println (dyn-when nil))
;; Statements, unchanged.
(when true (println "ran"))
(when false (println "not run"))
(println "end"))