Mark Engelberg <mark.engelb...@gmail.com> writes:

> Looks easy, but your dance and speak methods don't return a value
> which leaves a question in my mind...
>
> If the protocol implementation actually returns a value, do you have
> to explicitly typecast it in Java from Object into the desired type?

Yep, certainly.  A Clojure function can return any type so it has to be
that way generally.

However if you don't actually need the features of full protocols you
could use gen-interface instead:

    (ns interop.core)

    (gen-interface
     :name interop.core.Vocal
     :methods [[dance [Object] Object]
               [speak [] Object]
               [sum [int int] int]])

    (deftype Chorus [noise]
      interop.core.Vocal
      (dance [this boogy] (println "Shuffle like a" boogy))
      (speak [this] (println "I say there!  I can hear a" noise))
      (sum [this x y] (+ x y)))

Then the client becomes:

    import interop.core.Chorus;
    import interop.core.Vocal;

    public class Client {
        public static void main(String args[]) {
            Vocal voice = new Chorus("bird");
            voice.speak();
            voice.dance("pigeon");
            int x = voice.sum(1, 2);
            System.out.println("1 and 2 is " + x);
        }
    }

And so:

    $ java -cp $(lein classpath) Client
    I say there!  I can hear a bird
    Shuffle like a pigeon
    1 and 2 is 3

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