On 6 September 2015 at 14:41, Timothy Baldridge <tbaldri...@gmail.com>
wrote:

> >> "Variants fulfil the same purpose as key/value pairs in a map. The key
> denotes a context-sensitive purpose for the data, rather than its type."
>
> Then use a key/value type. That's my problem with this approach, it
> abuses a collection type and therefore creates confusion as to the type of
> data is contains.
>

I don't see why it "abuses a collection type". Vectors in Clojure are not
inherently homogenous, any more than maps are. If it's valid to use a bare
map to hold data, then it's equally valid to use a bare vector.

If you're going to argue that variants need to be wrapped in a type, then
surely that must apply to maps as well. Yet bare maps are used all the time
in Clojure. We don't need to enforce the shape of data with deftype and
defrecord; if necessary, we can do that with preconditions, runtime schema,
or static types.

That way I won't accidentally use the variant with conj, concat, count,
> pop, push, or the dozens of other vector functions that don't apply to
> variants at all.
>

You have the same problem with records; arbitrary keys can be added to the
structure.


> You also can't extend protocols to them without also applying those
> protocols to vectors of all sizes.
>

But if you don't need protocols, then there's no problem.


> In addition a variant deftype (or record) will only need 1 allocation each
> time you create it. Doing [:foo :bar] requires two allocations: one for the
> vector, and one for the tail array. In addition it imposes another pointer
> deref on every access as you have to jump from the type to the tail array
> to the value.
>

If that's your performance bottleneck, then optimise it with a deftype by
all means, but I imagine that in most cases that's not going to make a
significant difference.


> All in all, I see very little reason to use vectors as variants instead of
> a custom type, it complicates the code, and makes it harder to understand.
> Vectors aren't always vectors...they could also be variants?
>

Variants are just one use of vectors. Vectors are not homogenous
collections. It's perfectly valid to store a particular data value at a
particular index. For example, a coordinate [x y] is a perfectly valid use
for a vector in my book.

I'm not sure why you think that it "complicates the code, and makes it
harder to understand". All the examples I've seen of variants have been
exactly the opposite.

- James

-- 
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
--- 
You received this message because you are subscribed to the Google Groups 
"Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to clojure+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to