Extension methods vs. Traits

Daniel Latrémolière daniel.latremoliere at gmail.com
Fri Dec 11 02:33:48 PST 2009


Please can you explain how Scala has this problem. I don't know 
precisely Scala internals, but given web documentation, its 
implementation seems to transform a trait in one interface with all 
methods (even non-abstract) and one class for implementation. Then, the 
compiler need to modify each class extending the trait for adding 
methods delegating to the implementation of the trait (for implementing 
these abstract methods previously added by compiler).
http://hestia.typepad.com/flatlander/2009/01/scala-for-c-programmers-part-1a-mixins-and-traits-behind-the-scenes.html

In my suggest, I am reusing current proposal of declaration-site method 
but only in bytecode not in source. Then non-abstract methods of the 
trait becomes declaration-site extensions methods in interface and are 
not abstract methods to be implemented. Compiler doesn't modify each 
class extending the trait. The bytecode compatibility is the same than 
declaration-site because reusing it at bytecode level.

Thanks for details on Scala problem.
Daniel.


Alex Blewitt a écrit :
> Traits also suffer in Scala, because when the implementation of a 
> trait changes (such as bugfixing a method), the compiler has to be 
> re-run over all sources that use that trait. It's one of Scala's 
> biggest weaknesses that it is essentially a source-only compiled 
> language.
>
> Please don't lets follow the route of other lambda proposals and make 
> this more than it should be - but leave traits to a separate 
> implementation.
>
> Alex
>
> On 11 Dec 2009, at 09:45, Daniel Latrémolière wrote:
>
>> I would like to understand the final paragraph of the proposal where 
>> you are saying that the traits are much more significant change than 
>> extension methods to the language and VM.
>>
>> The VM doesn't need to implements traits. The compiler can transform 
>> one trait in one interface with abstract methods and 
>> declaration-sites for non-abstract methods and one anonymous class 
>> with the non-abstract methods coming from the trait and referenced in 
>> the interface by declaration-sites.
>> Then I don't think traits are a bigger change in VM than extension 
>> methods, because this can be only a language feature implemented by 
>> compiler using your declaration-site in bytecode.
>>
>> For the language, I think traits are much more readable, because code 
>> is local, and doesn't not ask to follow the delegate to the static 
>> method in another class file. I think traits are reusing much more 
>> current Java language (method syntax) and doesn't introduce a new 
>> construct like declaration-site extension methods. But, I have not 
>> studied all possible effects in Java language.
>>
>> Thanks for the explanations,
>> Daniel Latrémolière.
>>
>>
>
>



More information about the lambda-dev mailing list