Permalink
Browse files

Typing enhancements for ClojureWest demo

  • Loading branch information...
1 parent bf67742 commit c766d61ca863361d2959a9b35a131e8f3cdd6ade @ohpauleez committed Mar 15, 2013
Showing with 13 additions and 11 deletions.
  1. +5 −4 src/clojure/core/specs.clj
  2. +2 −2 src/clojure/core/specs/decorate.clj
  3. +6 −5 src/sterling/example.clj
@@ -1,7 +1,8 @@
(ns clojure.core.specs
(:require [clojure.test.generative.generators :as gen]
[clojure.test.generative.runner :as runner]
- [clojure.core.specs.decorate :as s-decorate]))
+ [clojure.core.specs.decorate :as s-decorate]
+ [clojure.core.typed :refer [ann ann-form]]))
(defn- extract-spec-args
"Specs have optional docstrings.
@@ -93,6 +94,7 @@
::ext (update-in ext-map [:constraints] normalize-constraints additional-constraints)
::doc spec-doc}))
+(ann defspec [Any Any * -> (HMap)])
(defmacro defspec
"Top-level macro to define spec maps and `def` them."
[name & raw-spec-args]
@@ -103,9 +105,8 @@
(throw (IllegalArgumentException. (str "Missing tags for " (seq (map first missing-tags)) " in " name))))
`(def ~name '~spec-map)))
-
-(def fn-with s-decorate/fn-with)
-#_(def fn-with
+(ann fn-with [(HMap) clojure.lang.Keyword * -> (Fn [Any -> Any])])
+(def fn-with
"Given a spec map,
produce a function that is decorated with the spec'd keywords
TODO: rewrite this doc string"
@@ -26,13 +26,13 @@
f))
;; TODO: Make this a proper, strong, type annotation
-(ann fn-with [Any -> (Fn [Any -> Any])])
+(ann fn-with [(HMap) clojure.lang.Keyword * -> (Fn [Any -> Any])])
(defn fn-with
"Given a spec map,
produce a function that is decorated with the spec'd keywords
TODO: rewrite this doc string"
[spec-map & kw-decs]
- (let [ret (reduce (fn [f decorate-kw] (decorate-fn decorate-kw spec-map f)) (::clojure.core.specs/f spec-map) kw-decs)]
+ (let [ret (reduce (fn [f decorate-kw] (decorate-fn decorate-kw spec-map f)) (:clojure.core.specs/f spec-map) kw-decs)]
(if (symbol? ret)
(resolve ret)
ret)))
@@ -1,8 +1,11 @@
(ns sterling.example
(:require [clojure.core.specs :as spec]
[clojure.test.generative.runner :as runner]
- [clojure.core.typed :refer [cf]]))
+ [clojure.core.typed :refer [cf ann]]))
+;; Even though `defspec` is typed to return a HMap,
+;; We still need to attach it to the var
+(ann tight-inc (HMap))
(spec/defspec tight-inc
inc
"An inc that only works for inputs 0-49"
@@ -104,14 +107,12 @@
(spec/example #'tight-inc)
- ;; TODO:
;; This fails, because it's looking at the var for info
(cf (pos-inc-fn 30))
;; This goes down the rabbit hole of typing hell
(cf ((spec/fn-with tight-inc :constraints :typed) 20))
- ;;TODO - auto add type information with a dispatch table based on tag values
- ;;TODO - hook in the core.contracts :constraints backend - currently just patching to :pre and :post
- ;;TODO - hook in Typed Clojure
+ ;;TODO - hook in the core.contracts :constraints backend back up- currently just patching to :pre and :post
+ ;;TODO - enahnce the core.typed decorator // ie: remove Any where possible
)

0 comments on commit c766d61

Please sign in to comment.