Is it possible to have a structure nested within a structure in Clojure? Consider the following code:
(defstruct rect :height :width)
(defstruct color-rect :color (struct rect))
(defn
#^{:doc "Echoes the details of the rect passed to it"}
echo-rect
[r]
(println (:color r))
(println (:height r))
(println (:width r)))
(def first-rect (struct rect 1 2))
;(def c-rect1 (struct color-rect 249 first-rect)) ;form 1
;output "249 nil nil"
(def c-rect1 (struct color-rect 249 1 2)) ;form 2
;output "Too many arguments to struct constructor
(echo-rect c-rect1)
Of course this is a contrived example but there are cases where I want to break a large data structure into smaller substructures to make code easier to maintain. As the comments indicate if I do form 1 I get "249 nil nil" but if I do form 2 I get "Too many arguments to struct constructor".
If I'm approaching this issue in the wrong way, please tell me what I should be doing. Searching the Clojure google group didn't turn up anything for me.
Edit:
I guess I wasn't as clear in the statement of my question as I thought I was:
1.) Is it possible to nest one struct within another in Clojure? (Judging from below that's a yes.)
2.) If so, what would be the correct syntax be? (Again, judging from below it looks like there are a few ways one could do this.)
3.) How do you fetch a value by a specified key when you've got a struct nested within another struct?
I guess my sample code didn't really demonstrate what I was trying to do very well. I'm adding this here so that others searching for this might find this question and its answers more easily.