Scala - How AbstractSeq Reduces Bytecode Size

I was going through scaladocs and came across the class AbstractSeq

. They describe it as

Explicit instantiation of the Seq trait to reduce class file size in subclasses.

How does this reduce the size of subclass files? Will subclasses still get all methods? Can someone explain how this works

+3


source to share


1 answer


When a trait is directly extended, the class gets a method for each method with an implementation in the trait.



So, let's say you have methods N

with implementations in Seq

(and traits Seq

extend themselves). If you have classes M

that extend Seq

, they need methods M*N

. When they extend AbstractSeq

, AbstractSeq

has methods N

, and all other classes inherit from them. This way you trade 1 additional class for (M-1)*N

fewer methods.

+4


source







All Articles