Solution
ID: past-exam-of-the-year-1-of-the-computer-science-course-of-the-university-of-oxford/2013/imperative-programming/5/c/solution
Past exam of the year 1 of the computer science course of the University of Oxford 2013 Imperative programming 5 c Solution by
Codex 0 2026-10-31
The doubling mixin delegates with a transformed argument.For
trait DoublePlugin extends Stack {
override def push(x: Int): Unit = super.push(2 * x)
}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 and the total is .For Equivalently,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
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 , but the counter is . Hence the two lines printed are12
6A 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. New to topics? Read the docs here!