(ns spec-test.core
(:require [clojure.spec :as s]))
(s/def :user/name string?)
(s/def :core/user (s/keys :req [:user/name]))
; A helper method to get better error messages.
; Also imagine that clojure.spec has a similar s/check
; function that looks similar to this one
; (used in our user-name function):
(defn check [type data]
(if (s/valid? type data)
true
(throw (AssertionError. (s/explain type data)))))
; ...how about if we could write our :pre and :post conditions like this:
(defn user-name [user :core/user]
(-> user :user/name)) :user/name
; ...so that they expands into this:
(defn user-name [user]
{:pre [(s/check :core/user user)]}
{:post [(s/check :user/name user)]}
(-> user :user/name))
; And if you have other :pre or :post conditions,
; then extend the existing ones:
(defn user-name [user :core/user number]
{:pre [pos? number]}
(-> user :user/name))
; ...becomes:
(defn user-name [user number]
{:pre [pos? number
(s/valid? :core/user user)]}
(-> user :user/name))
; a call to the function
(user-name {:user/name "Bill"} 3)
; Maybe it will be hard to find a nice syntax that works for the post condition,
; but the :pre condition should be doable I think!
--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to [email protected]
Note that posts from new members are moderated - please be patient with your
first post.
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
---
You received this message because you are subscribed to the Google Groups
"Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/d/optout.