On Nov 23, 3:24 pm, Zach Tellman <ztell...@gmail.com> wrote:
> On Nov 23, 12:12 pm, Chris Perkins <chrisperkin...@gmail.com> wrote:
> > I have only taken a quick look, so maybe I'm misunderstanding the
> > intent, but it's not clear to me how you would use this for sending
> > and receiving structured data from, say, a C program.
>
> > Taking your example from the wiki:
>
> > (def fr (compile-frame {:a :int16, :b :float32}))
>
> > Let's say I want to talk to a C program that speaks in structs, like
> > this:
>
> > struct Foo { short a; float b; }
>
> > The problem is, the C program cares about order - the short comes
> > before the float. How does the Clojure program know what order I need
> > the fields in, since I have specified the format with a map; an
> > unordered data structure? Is there another way to specify a structure
> > where order of the fields matters? If so, why have two ways of doing
> > it? Or am I just missing something?
> >
> Good question.  The solution didn't make the cut for my initial
> release, but will be added soon.  My plan is to have an (ordered-
> map ...) frame which encodes and decodes the keys in the given order.
> So for C interop, the frame would be
>
> (ordered-map :a :int16, :b :float32)
>
> An alternative would be to just turn any vector which is alternating
> keys and types into an ordered-map, but that seems a bit too magical.
>
> Zach
>

I would humbly submit that perhaps you should just go for ordered
formats, always. For example, a vector of two-element vectors could be
the canonical format:

[[:a :int16] [:b :float32]]

Then you could add some macro-sugar to make it pretty:

(struct :a :int16 :b :float32) => (compile-frame [[:a :int16]
[:b :float32]])

The reason I say this is both to simplify (have only one way to
specify a format), because I can envision people getting quite far
down a path of relying on the byte-ordering coming out of compile-
frame, simply due to the fact that clojure uses PersistentArrayMap for
small maps, giving the illusion that they are ordered. Then one day
they confidently add that Nth field to their struct definition (where
N appears to currently be 10), and suddenly they have byte-soup coming
out.

With always-ordered formats, there just seems to be less room for
error.

Just my 2c.

- Chris

-- 
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