logo       

Re: Extension as first class construct: msg#00118

lang.groovy.user

Subject: Re: Extension as first class construct

This is not like use as far as I can tell from the wiki.

In Objective-C you can define an extension to a class (called a Category) that adds methods to the class. Any instance of the class or a subclass will have the added methods.

@interface baseClass (category)

is how you declare it in Objective-C.

I have used this for example to add methods for serializing primitive values more directly than is supported by the NSCoder interface. In Java it would also be useful to be able to append an interface to an existing class that implements the required methods (via extension possibly). This would allow making classes serializable that are not so by default.

Since Groovy would not really be adding the methods to the class it would be good to support adding methods to all instances that implement an interface.

I could live with it being an executable statement, but it could also be a declaration that looks like a class.

Michael




On Oct 15, 2004, at 3:00 PM, Jeremy Rayner wrote:

On Thu, Oct 14, 2004 at 08:16:48PM -0700, Michael Latta wrote:
I would like to suggest that the extension of a class/interface be a
first class construct in Groovy.
The methods that are added to JDK
classes should use a language defined construct, which can also be used
by any user class. This allows a programmer using a binary library to
add methods to make the library classes polymorphic with custom
classes.
I am new to Groovy and from what I have read, the extensions
are currently performed by use of a meta model object that performs
dispatching, so it sounds like the mechanism is there. I am asking for
the mechanism to be accessible from the language level, not just the
library level.
I think you are describing the 'use' Groovy JDK method...

SomeThirdPartyClass.use {
doSomeOfMyStuff()
}

The Objective-C language has this construct, and it has
proven to be quite useful.
To which specific syntax do you refer?

In that context an extension is not able to
access any private parts of the class being extended. This would be
required in Groovy to prevent security breaches, and because you are
really not changing the .class file of the extended class.

You can do this to instances, which has some of those features
for example adding a closure to an instance of Foo...
---
class Foo {
property bar
}
foo = new Foo()

m = { println bar }
m.delegate = foo
m.call()



Thanks

Jez.





<Prev in Thread] Current Thread [Next in Thread>
Google Custom Search

News | FAQ | advertise