seeking rudimentary defprotocol + deftype help

39 views
Skip to first unread message

Raoul Duke

unread,
Dec 29, 2009, 3:08:06 PM12/29/09
to clo...@googlegroups.com
i haven't been able to figure out what i'm doing wrong yet. the error
message isn't super helpful :-}

Clojure 1.1.0-alpha-SNAPSHOT
user=> (defprotocol P (foo [x]))
P
user=> (deftype T [f] [P] (foo [x] x))
java.lang.ClassCastException: clojure.lang.Var cannot be cast to
java.lang.Class (NO_SOURCE_FILE:2)

?! thanks for any help.

Raoul Duke

unread,
Dec 29, 2009, 3:47:39 PM12/29/09
to clo...@googlegroups.com
p.s. extend seems to work
(extend ::T P {:foo (fn [x] (println x))})
(foo (T))

Rob Lachlan

unread,
Dec 29, 2009, 6:19:22 PM12/29/09
to Clojure
I'm also looking for some help on deftype, and rather than start
another thread, I thought I'd toss it in here. Basically, how do I
type hint for a deftype. For example, suppose I want a nested type,
something that looks like this:

(deftype A
[#^somePrimitive someField])

(deftype B
[#^A Avalue])

This brings up an error:

Unknown location:
error: java.lang.ClassNotFoundException: A

So how would I define a type that includes another type?

Raoul Duke

unread,
Dec 29, 2009, 7:01:34 PM12/29/09
to clo...@googlegroups.com
hi,

> So how would I define a type that includes another type?

my guess: type hints take java types, and you are giving a clojure
type. the docs for deftype say you can get a java type for the deftype
if you compile. ?

When AOT compiling, generates compiled bytecode for a class with the
given name (a symbol), prepends the current ns as the package, and
writes the .class file to the *compile-path* directory. When
dynamically evaluated, the class will have a generated name.

Meikel Brandmeyer

unread,
Dec 29, 2009, 7:38:12 PM12/29/09
to clo...@googlegroups.com
Hi,

Am 30.12.2009 um 00:19 schrieb Rob Lachlan:

> (deftype B
> [#^A Avalue])
>

> So how would I define a type that includes another type?

Did you try: (deftype B [#^::A Avalue])?

I don't know whether this works, but it's the obvious idea.

Sincerely
Meikel

Raoul Duke

unread,
Dec 30, 2009, 1:56:19 PM12/30/09
to clo...@googlegroups.com
> Did you try: (deftype B [#^::A Avalue])?
> I don't know whether this works, but it's the obvious idea.

http://stackoverflow.com/questions/1976423/nested-types-in-clojure

:-)

Raoul Duke

unread,
Dec 30, 2009, 4:41:51 PM12/30/09
to clo...@googlegroups.com
p.p.s. i was using -alpha- and changed to -new- (and pulled today) and
now i get a slightly different error. am i just flubbing the syntax in
some way i can't see for the trees?!

user=> (defprotocol P (foo [x]))
P

user=> (deftype A [] [P] (.foo [x]))
java.lang.RuntimeException: java.lang.ClassCastException:
clojure.lang.PersistentVector cannot be cast to clojure.lang.Symbol
(NO_SOURCE_FILE:0)


On Tue, Dec 29, 2009 at 12:08 PM, Raoul Duke <rao...@gmail.com> wrote:

Rob Lachlan

unread,
Dec 30, 2009, 5:40:29 PM12/30/09
to Clojure
Thanks alot guys!
Reply all
Reply to author
Forward
0 new messages