You can nest structs in structs like this:

(defstruct rect :height :width)
(defstruct colored-rect :color :shape)

(def subject (struct colored-rect :red (struct rect 50 30)))

(println subject) ; prints {:color :red, :shape {:height 50, :width
30}}

The defstruct macro takes a var and a bunch of keys for the struct.
Trying to do:
  (defstruct color-rect :color (struct rect))
means you're trying to pass in a rectangle struct itself as a key.

On Feb 16, 4:25 pm, Onorio Catenacci <catena...@gmail.com> wrote:
> Hi all,
>
> 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. I did search this group but I failed to turn up
> anything that seemed helpful.
>
> --
> Onorio Catenacci
--~--~---------~--~----~------------~-------~--~----~
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
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