= Solution
The doubling <mixin> delegates with a transformed argument.
``
trait DoublePlugin extends Stack {
override def push(x: Int): Unit = super.push(2 * x)
}
``
For `A`, the relevant <Scala trait linearization> is `A, DoublePlugin, SumPlugin, Stack`. A push first doubles the argument, then `SumPlugin` sees and sums that doubled value. The physical values are $2,4,6$ and the total is $12$.
For `B`, the <Scala trait linearization> is `B, SumPlugin, DoublePlugin, Stack`. `SumPlugin` sums the original argument, while its `super.push` doubles the stored value. The physical values are still $2,4,6$, but the counter is $1+2+3=6$. Hence the two lines printed are
``
12
6
``
Equivalently,
$$
\boxed{a.sum=12,\qquad b.sum=6.}
$$
This is <stackable trait modification>: the order determines which arguments an outer <mixin> observes. In particular, the running-total specification from part (b) holds for `A` but does not survive the transforming inner <mixin> in `B`. With the given implementation, popping from `B` would subtract the doubled stored value, demonstrating why mixing individually sensible <traits> can break a combined <representation invariant>.
Back to article page