gen-class patch

0 views
Skip to first unread message

Chouser

unread,
Jul 27, 2008, 9:35:27 PM7/27/08
to clo...@googlegroups.com
Here is a tiny little patch against gen-class.clj.

The only thing different is that it will require any methods or
functions you have in your class namespace start with an underscore if
you want them to hook up to method names or other names given in your
gen-and-*-class call.

Here's the smallest example I could come up with:

(gen-and-load-class 'chouser.MiniTest :init 'myinit)
(in-ns 'chouser.MiniTest)
(clojure/refer 'clojure)
(defn myinit [] (prn "hi"))

That's the old way. To fix that to work with this patch, replace myinit with:

(defn _myinit [] (prn "hi"))

The benefit of this is that your chances of a name conflict on the
(clojure/refer 'clojure) call (or any other refer) goes from "very
likely" to "almost zero".

This name mangling was discussed on IRC:
http://clojure-log.n01se.net/date/2008-07-25.html#15:31c-16:17a

It's entirely Rich's idea, but of course I don't speak for Rich and
can't say if this implementation or even this type of solution will be
incorporated into Clojure proper.

I barely know what's going on in gen-class, but the attached example
seems to work. What do you all think?

--Chouser

gen-class-underscore.patch
scratch04.clj

Chouser

unread,
Jul 28, 2008, 1:11:08 PM7/28/08
to clo...@googlegroups.com
After further discussion in IRC, here is a different patch.

Before either of these patches:

(gen-and-load-class 'net.n01se.MiniTest :init 'myinit)
(in-ns 'net.n01se.MiniTest)


(clojure/refer 'clojure)
(defn myinit [] (prn "hi"))

With this latest patch:

(gen-and-load-class 'net.n01se.MiniTest :init 'myinit)
(clojure/in-ns 'net.n01se)
(clojure/refer 'clojure)
(defn MiniTest-myinit [] (prn "hi"))

Note that for the same class name, the namespace is different -- it
now indicates just the package name (net.n01se instead of
net.n01se.MiniTest). The classname is now included
with each function name (MiniTest-myinit instead of myinit).

--Chouser

genclass-classname.patch
scratch04.clj

Rich Hickey

unread,
Jul 28, 2008, 1:30:11 PM7/28/08
to Clojure
Thanks!

This patch has been applied, SVN rev 968.

Rich

Chouser

unread,
Jul 28, 2008, 1:45:11 PM7/28/08
to clo...@googlegroups.com
On Mon, Jul 28, 2008 at 1:11 PM, Chouser <cho...@gmail.com> wrote:
>
> Note that for the same class name, the namespace is different -- it
> now indicates just the package name (net.n01se instead of
> net.n01se.MiniTest). The classname is now included
> with each function name (MiniTest-myinit instead of myinit).

Sorry, I forgot to update the doc string. Also the examples in the
(comment) at the end are now completely out of date. This subsequent
patch fixes both.

--Chouser

doc-and-example.patch
Reply all
Reply to author
Forward
0 new messages