- About Scala
- Documentation
- Code Examples
- Software
- Scala Developers
Dynamic traits
Tue, 2009-07-14, 02:01
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
Tue, 2009-07-14, 03:07
#2
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.
>>
>>
>
>
Tue, 2009-07-14, 08:57
#3
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.spivak@gmail.com> wrote:
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.spivak@gmail.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.spivak@gmail.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.
Tue, 2009-07-14, 10:57
#4
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@gmail.com>
2009/7/14 Daniel Sobral <dcsobral@gmail.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.spivak@gmail.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.
Things like methods defined inside a trait are actually inlined in the class.
On Mon, Jul 13, 2009 at 10:01 PM, victorspivak <victor.spivak@gmail.com> wrote:
--
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.