Skip to content

Instantly share code, notes, and snippets.

@frenchy64
Created August 10, 2012 13:52
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save frenchy64/3314344 to your computer and use it in GitHub Desktop.
Save frenchy64/3314344 to your computer and use it in GitHub Desktop.
F-bounded Polymorphism in Typed Clojure
; y has upper bound x, z has upper bound (Seqable y)
typed.core=> (ann foo (All [x [y :< x] [z :< (Seqable y)]]
[x y z -> Any]))
[typed.core/foo (All [x [y :< x] [z :< (clojure.lang.Seqable y)]] (Fn [x y z -> Any]))]
typed.core=> (declare foo)
#'typed.core/foo
;cf = check-form
typed.core=> (cf (foo 2 2 [2]))
Any
typed.core=> (cf (foo 2 2 [1]))
#<AssertionError java.lang.AssertionError: Assert failed: Inferred type (Vector* (Value 1)) is not between bounds Nothing and (clojure.lang.Seqable (Value 2))
(and (subtype? inferred upper-bound) (subtype? lower-bound inferred))>
typed.core=> (cf (foo 2 1 [2]))
#<AssertionError java.lang.AssertionError: Assert failed: Inferred type (Vector* (Value 2)) is not between bounds Nothing and (clojure.lang.Seqable (Value 1))
(and (subtype? inferred upper-bound) (subtype? lower-bound inferred))>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment