Hi David,

I've used protocols for the exact same purpose (Thrift unpacking, and like
you (?), for convenience in Cascalog). I think it works very well and is
speedier than other methods. It is also convenient when you have nested
data structures and you don't want to care how to go through this nesting
to get nested values. Protocols can make this work like magic. Your
question makes sense though and I wasn't sure if it will always works
(never had a counter example in practise though). I did the following
experiment and I think it shows that it works without exception:

user=> (isa? String Object)
true

(defprotocol ITest
  (show-me [v]))

(extend-protocol ITest
  Object
  (show-me [v] (println "got an object"))
  String
  (show-me [v] (println "got a string")))

user=> (show-me (Object.))
got an object
nil
user=> (show-me (String.))
got a string
nil
user=> (show-me ^Object (String.))
got a string
nil
user=> (show-me (Integer. 1))
got an object
nil
user=> (show-me (cast Object "1"))
got a string
nil

Not sure if there are other tests available, but this gives me some
confidence.

HTH,

Jeroen


On Thu, May 9, 2013 at 8:45 PM, Dave Kincaid <kincaid.d...@gmail.com> wrote:

> I've not worked with protocols much, but saw a good fit recently. However,
> I'm a little bit unsure about this situation. I have some Thrift objects
> that I'd like to be able to easily unpack into maps, so I created a protocol
>
> (defprotocol Unpackable
>   (unpack [x]))
>
> Thrift has two main data types - structs and unions that need to be
> handled differently. Structs always implement the interface TBase. Unions
> extend the abstract class TUnion which in turn implements the interface
> TBase. So I'm extending my protocol to these types like this:
>
> (extend-protocol IUnpackable
>   TUnion
>   (unpack [x] (unpack-union x))
>
>   TBase
>   (unpack [x] (unpack-struct x (get-meta-data (class x)))))
>
> It all seems to work correctly, but I'm a little unsure that it is
> guaranteed to work especially in the case of a union. Since a union extends
> TUnion, but also implements TBase will a call to (unpack the-union) do the
> right thing (go to unpack-union instead of unpack-struct) every time?
>
> Thanks.
>
> --
> --
> 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/groups/opt_out.
>
>
>

-- 
-- 
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/groups/opt_out.


Reply via email to