- About Scala
- Documentation
- Code Examples
- Software
- Scala Developers
Re: Is Dynamic Object Decoration Possible
This is generally what implicit conversions are used for.
trait MyTrait[T] {
def myObject : T
def myMethod = {
//...
}
}
implicit def decorate[T](o : T) : MyTrait[T] = new MyTrait[T] {
override def myObject = o
}
Then, whenever you have an instance of whatever the decorated type is, you can do
val o = new Foo()
o.myMethod
Kris
On Fri, Jan 16, 2009 at 10:30 AM, Bastian, Mark <mbastia [at] sandia [dot] gov> wrote:
trait MyTrait[T] {
def myObject : T
def myMethod = {
//...
}
}
implicit def decorate[T](o : T) : MyTrait[T] = new MyTrait[T] {
override def myObject = o
}
Then, whenever you have an instance of whatever the decorated type is, you can do
val o = new Foo()
o.myMethod
Kris
On Fri, Jan 16, 2009 at 10:30 AM, Bastian, Mark <mbastia [at] sandia [dot] gov> wrote:
Scala gives you the powerful ability to create object factories that decorate the primary object wth a trait like so with no explicit class definition: def objectWithTrait = new MyObject with MyTrait Is it possible to dynamically decorate an object, like so: def decorate(o : Object) : Object = o with MyTrait I suppose you could make a cloning factory that does something like this: def cloneAndDecorate(o : MyObject) { //foo and bar are copy constructor fields new o(o.foo, o.bar) with MyTrait } This might be adequate in some situations, but it might be nice to be able to dynamically add the trait. Can it be done? Thanks, Mark










Re: Is Dynamic Object Decoration Possible
One sollution that might work right now is to use a DynamicProxy[1] to wrap the object in and cast it to T with MyTrait.
If I'm not mistaken Scala generates Interface instances for most things, and as long as you can pass those instances to the proxy factory you should be fine.
BR, John
[1] http://java.sun.com/j2se/1.3/docs/guide/reflection/proxy.html
On Fri, Jan 16, 2009 at 7:14 PM, Kris Nuttycombe <kris [dot] nuttycombe [at] gmail [dot] com> wrote:
Re: Is Dynamic Object Decoration Possible
Or do you want to pass the resulting object out of that scope and be able to reflect upon all of its types in the receiving scope? I don't think there's a way to do that.
Kris
On Fri, Jan 16, 2009 at 2:38 PM, John Nilsson <john [at] milsson [dot] nu> wrote:
Re: Is Dynamic Object Decoration Possible
You're right, didn't think of that. I don't think that works to well with overriding though. Using dynamix proxy it should be possible. The returned object is just a facade implementing all the interfaces passed to the proxy factory.
It was a while ago now, but I was trying this approach to be able to implement JOIN in my own SQL API. At the time I think I had some probles getting the instances of the interfaces needed to join structural types or something like that.
BR,John