Hi Albert,

I made sense of this by keeping the concept of defprotocol separate
from definterface and deftype. defprotocol is like multimethods and
deftype is like creating an implementation of an interface. You then
need to instantiate a new instance of the deftype to execute its
methods. The following repl session shows this;

./repl.sh
Clojure 1.2.0-master-SNAPSHOT
user=> (set! *warn-on-reflection* true)
true
user=> (defprotocol PTest (test-it [msg]))
PTest
user=> (extend-protocol PTest nil (test-it[msg] "Nix"))
nil
user=> (extend-protocol PTest String (test-it[msg] msg))
nil
user=> (definterface ITest (test_it ^boolean [^String msg]))
user.ITest
user=> (deftype Test [] ITest (test-it [this msg] (> (.length msg) 0)))
user.Test
user=> (test-it nil)
"Nix"
user=> (test-it "two")
"two"
user=> (let [tt (Test.)] (.test-it tt "hello"))
true

Note where you do and don't need type hints (there are no reflection
warnings). Also note that I used test_it in the definterface.

There are probably ways of creating types on protocols, but I haven't
tried that yet.

-Hth, Adrian

On Tue, Aug 31, 2010 at 11:20 PM, Albert Cardona <sapri...@gmail.com> wrote:
> Hi all,
>
> I am puzzled by the type hint support in deftype.
> I can add type hints to defprotocol:
>
> user=> (defprotocol PTest
>  (test-it ^boolean [this ^String msg]))
> PTest
>
> … but adding them to deftype fails:
>
>
> user=> (deftype Test [^String name]
>  PTest
>  (test-it [this msg] (str "test " name ": " msg \space (.length msg))))
> java.lang.IllegalArgumentException: Can't find matching method:
> test_it, leave off hints for auto match. (NO_SOURCE_FILE:1)
>
>
> So I write the deftype without type hints:
>
> user=> (deftype Test [^String name]
>  PTest
>  (test-it [this msg] (str "test " name ": " msg \space (.length msg))))
> user.Test
>
>
> But then, I get reflection warnings:
>
> user=> (set! *warn-on-reflection* true)
> true
>
> user=> (test-it (Test. "one") "two")
> "test one: two 3"
>
> 8/31/10 11:11:49 PM     [0x0-0x29029].org.fiji[382]     Reflection warning,
> NO_SOURCE_PATH:3 - reference to field length can't be resolved.
>
> In any case "length" is a method, not a field, in String.
>
> The above is just a minimal example. I am experiencing this issue in
> just about all deftype that I need to type-hint.
>
> The error when type-hinting suggests that the latter is unnecessary,
> that it could be deduced from the hints in the protocol
> definition--but that is not the case.
>
> What am I missing? Thanks for any help.
>
> Albert
>
> --
> http://albert.rierol.net
>

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