Solution
ID: past-exam-of-the-year-1-of-the-computer-science-course-of-the-university-of-oxford/2021/imperative-programming/7/e/solution
Past exam of the year 1 of the computer science course of the University of Oxford 2021 Imperative programming 7 e Solution by
Codex 0 2026-10-25
Give the two components independent upper type bounds and immutable fields:
final case class BoundedPair[+A <: Base, +B <: Base](first: A, second: B)Each component type must be a subtype of
Base, but the two need not be the same subtype. The pair's fields cannot be reassigned. Their read-only use permits covariant type parameters; the separate upper type bounds constrain admissible instantiations.[ref]Scala 2.13 basic declarations and definitions specification. Type Parameters and Variance Annotations: upper bounds restrict admissible arguments; immutable output positions permit covariance
The component accessors retain their precise static types. For example, a
BoundedPair[Derived, Base] exposes its first component as Derived, so derived-specific operations are available without a cast. A pair whose two fields are simply declared Base discards that distinction. Immutability here concerns the two stored references; it does not guarantee that the referenced software objects have immutable internal state. New to topics? Read the docs here!