OK, I'm trying to get seriously stuck in, and the first thing I'm
trying to do is reimplement an inference engine I first wrote in
Portable Standard Lisp and then in InterLisp-D in /idiomatic/ Clojure.
So please have patience with me...

If one has something which is strongly typed, is it idiomatic to make
a Java class to hold it?

I want a structure like this

(defstruct feature :name :default)

but I want in addition to constrain the binding of :name to be always
a Clojure keyword and the binding of :default to be always a boolean -
the Clojure values either true or false.

I've tried

(defstruct feature [#^Keyword :name] :default)

but the reader spits an illegal argument exception. Is there different
syntax which the reader could parse? Or am I using the wrong kind of
thing?

Of course given that a struct is immutable I could have a make-feature
function which has type-hinted args:

(defn
        #^{:doc "Make a feature safely"}
        make-feature [#^Keyword name #^bool default]
        (struct-map feature :name name :default default))

This works and does sort-of what I want - I can't guarantee that
nothing outside my make-feature function will make features but I
don't think I need to. However, it feels clunky - I instinctively feel
there must be a better way. Is this what deftype should be used for?
If so, where is it documented? The book I'm using - Stuart Halloway's
'Programming Clojure' - doesn't seem to have it (or if it has I've
missed it). Also, I don't seem to have deftype in my 1.1.0 rc3 build :

cc.journeyman.dtree.dtree=> (doc deftype)
java.lang.Exception: Unable to resolve var: deftype in this context
(NO_SOURCE_FILE:20)

I've also grepped the source code of clojure and failed to find it.

While we're on this point I'm delighted with the #^{:doc ...}
metadata! The way this works with the (doc thing) function is elegant
and useful. I'm also delighted with the way that keywords and maps are
functions onto each other:

cc.journeyman.dtree.dtree=> (def alive (make-feature :Alive true))
#'cc.journeyman.dtree.dtree/alive
cc.journeyman.dtree.dtree=> (:name alive)
:Alive
cc.journeyman.dtree.dtree=> (alive :name)
:Alive

This is very, very pretty.






-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en

Reply via email to