NullPointerException on disj

51 views
Skip to first unread message

Allen Johnson

unread,
May 27, 2010, 1:10:09 AM5/27/10
to clo...@googlegroups.com
Hey everyone. I was playing around with the protocols/deftype stuff
and ran into a weird NullPointerException when calling the satisfies?
function. Seems to only happen with a java.lang.Object instance.

Clojure 1.2.0-master-SNAPSHOT
user=> (defprotocol Greeter (greet [this]))
Greeter
user=> (satisfies? Greeter nil)
false
user=> (satisfies? Greeter "")
false
user=> (satisfies? Greeter (Object.))
java.lang.NullPointerException (NO_SOURCE_FILE:0)

Narrowed it down to this function:

;; core_deftype.clj
(defn find-protocol-impl [protocol x]
(if (instance? (:on-interface protocol) x)
x
(let [c (class x)
impl #(get (:impls protocol) %)]
(or (impl c)
(and c (or (first (remove nil? (map impl (butlast (super-chain c)))))
(when-let [t (reduce pref (filter impl (disj
(supers c) Object)))]
(impl t))
(impl Object)))))))

More specifically, here:

(disj (supers c) Object)

Since in this case, `c` is java.lang.Object, supers returns nil which
disj doesn't seem to like. Shouldn't disj handle nil gracefully?

Thanks,
Allen

Rich Hickey

unread,
May 27, 2010, 8:17:32 AM5/27/10
to Clojure

On May 27, 1:10 am, Allen Johnson <akjohnso...@gmail.com> wrote:
> Hey everyone. I was playing around with the protocols/deftype stuff
> and ran into a weird NullPointerException when calling the satisfies?
> function. Seems to only happen with a java.lang.Object instance.
>
> Clojure 1.2.0-master-SNAPSHOT
> user=> (defprotocol Greeter (greet [this]))
> Greeter
> user=> (satisfies? Greeter nil)
> false
> user=> (satisfies? Greeter "")
> false
> user=> (satisfies? Greeter (Object.))
> java.lang.NullPointerException (NO_SOURCE_FILE:0)
>
> Narrowed it down to this function:
>
> ;; core_deftype.clj
> (defn find-protocol-impl [protocolx]

>   (if (instance? (:on-interfaceprotocol) x)


>     x
>     (let [c (class x)

>           impl #(get (:implsprotocol) %)]


>       (or (impl c)
>           (and c (or (first (remove nil? (map impl (butlast (super-chain c)))))
>                      (when-let [t (reduce pref (filter impl (disj
> (supers c) Object)))]
>                        (impl t))
>                      (impl Object)))))))
>
> More specifically, here:
>
> (disj (supers c) Object)
>
> Since in this case, `c` is java.lang.Object, supers returns nil which
> disj doesn't seem to like. Shouldn't disj handle nil gracefully?
>

Yes, could you please enter a ticket for this?

Thanks,

Rich

Allen Johnson

unread,
May 27, 2010, 9:54:05 AM5/27/10
to clo...@googlegroups.com
Reply all
Reply to author
Forward
0 new messages