http://scala-programming-language.1934581.n4.nabble.com/new-A-with-B-how-to-do-this-starting-from-classA-td1941976.html
http://scala-programming-language.1934581.n4.nabble.com/Mixin-in-traits-in-instance-objects-td1948193.html
http://stackoverflow.com/questions/3254232/dynamic-mixin-in-scala-is-it-possible
but... there is a compiler plugin that can help you work around it:
https://github.com/scala-incubator/autoproxy-plugin/wiki/
https://github.com/scala-incubator/autoproxy-plugin/wiki/Use-Case---Dynamic-Mixins // !
best, -sciss-
the short answer is no. see for example
http://scala-programming-language.1934581.n4.nabble.com/new-A-with-B-how-to-do-this-starting-from-classA-td1941976.html
http://scala-programming-language.1934581.n4.nabble.com/Mixin-in-traits-in-instance-objects-td1948193.html
http://stackoverflow.com/questions/3254232/dynamic-mixin-in-scala-is-it-possible
but... there is a compiler plugin that can help you work around it:
https://github.com/scala-incubator/autoproxy-plugin/wiki/
https://github.com/scala-incubator/autoproxy-plugin/wiki/Use-Case---Dynamic-Mixins // !
best, -sciss-
On 8 Apr 2011, at 03:17, linjie nie wrote:
> Hi list -
>
> As a newbie, I know I can mixin a trait when create a object, val foo=new Foo() with Bar.
> My question is, can I mixin a trait to a object, which is created by someone else?
>
> val foo=new Foo()
> ....
> val foo2=foo with Bar //error
>
> Thanks a lot
> Jason
best, -sciss-
very interesting - can you outline why it's called Autoproxy-Lite? has it somehow reduced functionality?
best, -sciss-
example:
val foo = new Foo()
case class RichFoo(foo: Foo) extends Bar
implicit def foo2RichFoo(foo: Foo): RichFoo = RichFoo(foo)
implicit def richFoo2Foo(richFoo: RichFoo): Foo = richFoo.foo
now you can simply do
val foo2: RichFoo = foo
val foo: Bar = foo
you can also simply call foo.methodInBar and the implicit will take
care of the rest.
Be aware that this will always happen when implicits are in scope
-Stefan
2011/4/8 linjie nie <niel...@gmail.com>:
2011/4/8 Kevin Wright <kev.lee...@gmail.com>:
I'm not an expert on the Scala type system yet, so I haven't worked this out yet, but it vaguely seems to me that there should be a way to do this without relying on implicits by using traits and self types..
best, -sciss-