Pre/post conditions with records and types

143 views
Skip to first unread message

Daniel Solano Gómez

unread,
Mar 16, 2011, 11:20:51 AM3/16/11
to cloju...@googlegroups.com
Hello, all,

I do not know the extent to which Clojure pre- and post-conditions are used
in the real world, but I was wondering if there was any interest in
trying to integreate this functionality better with protocols.

Currently, trying to define pre/post-conditions in a defprotocol seems
to have no effect:

(defprotocol Foo
(foo [x]
"The foo function."
; compiles but seems to have no effect
{:post [(string? %)]}))

-----

The extend-type and extend-protocol macros support this due to their
expansion of fn forms:

(extend-type Integer
Foo
(foo [x]
{:post [(string? %)]}
"Integer!"))

(extend-protocol Foo
Double
(foo [x]
{:post [(string? %)]}
"Double!"))

-------


However, deftype and defrecord will fail to compile if trying to use a
post-condition with a % symbol:

(deftype FooType []
Foo
(foo [x]
; fails to compile
#_{:post [(string? %)]}
"FooType!"))

(defrecord FooRecord [a b c]
Foo
(foo [x]
; fails to compile
#_{:post [(string? %)]}
"FooRecord"))

-----

I think that adding pre-/post-condition support to protocols has some
value. It can add some semantics which may be intented as part of the
protocol but can not currently be communicated outside of the docstring.
If there is any interest in this, I could look into implementing this
and submitting a patch.

Sincerely,

Daniel Solano Gómez

Reply all
Reply to author
Forward
0 new messages