Skip to content

Commit

Permalink
tweaks to type predicates
Browse files Browse the repository at this point in the history
  • Loading branch information
akkartik committed Feb 23, 2015
1 parent 2c12a9e commit 4cfe8c3
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 10 deletions.
15 changes: 7 additions & 8 deletions arc.arc
Expand Up @@ -1948,18 +1948,17 @@ of tables."

; Optional predicate-based typing

(assign types* (table))
(= type-predicates* (table))

(mac deftype (name . body)
(mac def-isa (name . body)
"Declares a new predicate-based type that can be checked with 'isa'."
`(= (types* ',name) (fn (_) ,@body)))
`(= (type-predicates* ',name) (fn (_) ,@body)))

(redef isa (x y)
"Is 'x' of type 'y'?"
(if (is (type x) y) t
(let valid nil
(maptable (fn (k v) (if (is k y) (set valid))) types*)
(and valid ((types* y) x)))))
"Is 'x' of type 'y'? Also checks for predicate matches in types* table."
(or (is type.x y)
(aand (type-predicates* y)
(it x))))

(def copylist (xs)
(if acons.xs
Expand Down
4 changes: 2 additions & 2 deletions arc.arc.t
Expand Up @@ -91,12 +91,12 @@
(suite isa
checks-type (isa "abc" 'string)
checks-type-predicate (do (assert-nil (isa 1 'positive-num))
(deftype positive-num
(def-isa positive-num
(and (or (isa _ 'num) (isa _ 'int))
(> _ 0)))
(assert (isa 1 'positive-num) "isa supports predicate types")
; cleanup
(wipe (types* 'positive-num))
(wipe (type-predicates* 'positive-num))
(assert-nil (isa 1 'positive-num) "isa test suite failed to cleanup")))

(suite coerce
Expand Down

0 comments on commit 4cfe8c3

Please sign in to comment.