What the recommended way now to create an instance and override its methods?

259 views
Skip to first unread message

Hussein B.

unread,
Jun 18, 2013, 3:45:14 AM6/18/13
to clo...@googlegroups.com
Hi,

I know we usually use 'proxy' macro when we want to create an instance of a concrete class and override some of its methods. But do we have now a recommended approach?  

AFAIK, reify only works with protocols and interfaces, can't be used to create an instance and override its methods.

I'm a little bit worry about 'proxy' macros since some devs are saying that it has a performance penalty.

Thanks for help and time.

Jim

unread,
Jun 18, 2013, 7:16:40 AM6/18/13
to clo...@googlegroups.com
On 18/06/13 08:45, Hussein B. wrote:
> I know we usually use 'proxy' macro when we want to create an instance
> of a concrete class and override some of its methods. But do we have
> now a recommended approach?

'proxy' is the recommended approach unless you have some weird
overriding requirements like providing an override based on the type of
the arguments rather than the arity, in which case you have to drop to
Java and manually construct your proxy...

yes, 'reify' will be slightly faster but as you say it only works with
abstractions and not concrete objects.

hope that helps, :)

Jim

Hussein B.

unread,
Jun 18, 2013, 7:34:24 AM6/18/13
to clo...@googlegroups.com
Since reify allows us to override methods of an object, it is better to do:

(def meh-object (MehClass.))

(reify meh-object
  ;; override methods)

than:

(proxy MehClass[]
  ;; override methods)

Thanks.

Jim

unread,
Jun 18, 2013, 7:41:57 AM6/18/13
to clo...@googlegroups.com
On 18/06/13 12:34, Hussein B. wrote:
Since reify allows us to override methods of an object, it is better to do:

'reify' allows you to implement/satisfy interface-methods - not to override concrete ones.

user=> (reify String
  #_=>  (length [this] (count this)))

user=> CompilerException java.lang.IllegalArgumentException: only interfaces are supported, had: java.lang.String, compiling:(NO_SOURCE_PATH:1:1)

Jim

Hussein B.

unread,
Jun 18, 2013, 7:43:56 AM6/18/13
to clo...@googlegroups.com
Hmm,
then why Clojure docs http://clojuredocs.org/clojure_core/clojure.core/reify mentions 'object':

protocol-or-interface-or-Object
(methodName [args+] body)*

Cedric Greevey

unread,
Jun 18, 2013, 7:47:05 AM6/18/13
to clo...@googlegroups.com
That's the *class* Object, with a capital O. It's the one concrete class that can be extended and its methods overridden using reify (or deftype).


--
--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clo...@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+u...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
---
You received this message because you are subscribed to the Google Groups "Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email to clojure+u...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Jim

unread,
Jun 18, 2013, 7:48:59 AM6/18/13
to clo...@googlegroups.com
as Cedric pointed out, it means the java.lang.Object class...not any object

Jim

Gary Trakhman

unread,
Jun 18, 2013, 11:01:11 AM6/18/13
to clo...@googlegroups.com
I think you need to read something comprehensive before any of these replies start to make sense, I would recommend Joy Of Clojure, or some good blog posts.

This might be a good starting point with some links for further reading:

To understand the issues fully will take a little effort.


On Tue, Jun 18, 2013 at 7:43 AM, Hussein B. <hubag...@gmail.com> wrote:

--
Reply all
Reply to author
Forward
0 new messages