This page is no longer maintained — Please continue to the home page at www.scala-lang.org

Dynamic traits

4 replies
victorspivak
Joined: 2009-07-14,
User offline. Last seen 42 years 45 weeks ago.

Is it possible to instantiate an objects with traits using core reflection? I
would like to attach traits to some object instances using factory pattern
that driven based on external configuration.

Thanks

dcsobral
Joined: 2009-04-23,
User offline. Last seen 38 weeks 5 days ago.
Re: Dynamic traits
No. A trait does not exist as a JVM entity, unless it's equivalent to an interface.
Things like methods defined inside a trait are actually inlined in the class.

On Mon, Jul 13, 2009 at 10:01 PM, victorspivak <victor [dot] spivak [at] gmail [dot] com> wrote:

Is it possible to instantiate an objects with traits using core reflection? I
would like to attach traits to some object instances using factory pattern
that driven based on external configuration.

Thanks
--
View this message in context: http://www.nabble.com/Dynamic-traits-tp24472063p24472063.html
Sent from the Scala - User mailing list archive at Nabble.com.




--
Daniel C. Sobral

Something I learned in academia: there are three kinds of academic reviews: review by name, review by reference and review by value.
victorspivak
Joined: 2009-07-14,
User offline. Last seen 42 years 45 weeks ago.
Re: Dynamic traits

I know that traits do not exist in JVM. I have implemented a similar to
traits facility in Java. I have created a custom classloader and I use
JavaAssist for byte code manipulation. It uses the class hierarchy
linearization approach as well. I thought that Scala could provide a cleaner
way to do this job :( I hope at some point Scala will provide extended
support for core reflection. It will be very nice.

Victor

Daniel Sobral wrote:
>
> No. A trait does not exist as a JVM entity, unless it's equivalent to an
> interface.
> Things like methods defined inside a trait are actually inlined in the
> class.
>
> On Mon, Jul 13, 2009 at 10:01 PM, victorspivak
> wrote:
>
>>
>> Is it possible to instantiate an objects with traits using core
>> reflection?
>> I
>> would like to attach traits to some object instances using factory
>> pattern
>> that driven based on external configuration.
>>
>> Thanks
>> --
>> View this message in context:
>> http://www.nabble.com/Dynamic-traits-tp24472063p24472063.html
>> Sent from the Scala - User mailing list archive at Nabble.com.
>>
>>
>
>

Kevin Wright
Joined: 2009-06-09,
User offline. Last seen 49 weeks 4 days ago.
Re: Dynamic traits
Under the covers, traits are implemented in the JVM as an interface / class pair, with the class holding any concrete methods implemented for that class.

I'm currently playing around with writing a compile plugin that can generate the necessary methods that forward calls to an appropriate delegate object.  This should give the best possible performance when the JVM has done its magic and inlined the necessary code :)

Given that traits are interfaces, you could also use the dynamic proxying support already built directly into java:
http://java.sun.com/j2se/1.3/docs/guide/reflection/proxy.html
For this to work, you'll also need to implement a DelegationHandler, as detailed here:
http://www.developertutorials.com/tutorials/java/java-dynamic-proxies-050514/page5.html

It should be faily simple to wrap this all up in not-very-many lines of Scala code.  It'll solve the problem quite quickly, but it does use reflection, with all the possible type-safety and performance issues that that entails.


On Tue, Jul 14, 2009 at 3:00 AM, victorspivak <victor [dot] spivak [at] gmail [dot] com> wrote:

I know that traits do not exist in JVM.  I have implemented a similar to
traits facility in Java.  I have created a custom classloader and I use
JavaAssist for byte code manipulation.  It uses the class hierarchy
linearization approach as well. I thought that Scala could provide a cleaner
way to do this job :( I hope at some point Scala will provide extended
support for core reflection.  It will be very nice.

Victor


Daniel Sobral wrote:
>
> No. A trait does not exist as a JVM entity, unless it's equivalent to an
> interface.
> Things like methods defined inside a trait are actually inlined in the
> class.
>
> On Mon, Jul 13, 2009 at 10:01 PM, victorspivak
> <victor [dot] spivak [at] gmail [dot] com>wrote:
>
>>
>> Is it possible to instantiate an objects with traits using core
>> reflection?
>> I
>> would like to attach traits to some object instances using factory
>> pattern
>> that driven based on external configuration.
>>
>> Thanks
>> --
>> View this message in context:
>> http://www.nabble.com/Dynamic-traits-tp24472063p24472063.html
>> Sent from the Scala - User mailing list archive at Nabble.com.
>>
>>
>
>
> --
> Daniel C. Sobral
>
> Something I learned in academia: there are three kinds of academic
> reviews:
> review by name, review by reference and review by value.
>
>

--
View this message in context: http://www.nabble.com/Dynamic-traits-tp24472063p24472491.html
Sent from the Scala - User mailing list archive at Nabble.com.


ounos
Joined: 2008-12-29,
User offline. Last seen 3 years 44 weeks ago.
Re: Dynamic traits
Such a utility would be very powerful though and nice to have, as it would narrow the gap of what is possible at compile time and what is possible at runtime. (That would some analog of javax.tools.JavaCompiler at least)

2009/7/14 Daniel Sobral <dcsobral [at] gmail [dot] com>
No. A trait does not exist as a JVM entity, unless it's equivalent to an interface.
Things like methods defined inside a trait are actually inlined in the class.

On Mon, Jul 13, 2009 at 10:01 PM, victorspivak <victor [dot] spivak [at] gmail [dot] com> wrote:

Is it possible to instantiate an objects with traits using core reflection? I
would like to attach traits to some object instances using factory pattern
that driven based on external configuration.

Thanks
--
View this message in context: http://www.nabble.com/Dynamic-traits-tp24472063p24472063.html
Sent from the Scala - User mailing list archive at Nabble.com.




--
Daniel C. Sobral

Something I learned in academia: there are three kinds of academic reviews: review by name, review by reference and review by value.

Copyright © 2012 École Polytechnique Fédérale de Lausanne (EPFL), Lausanne, Switzerland