Reusable behavior composed into a host class in object-oriented programming, often through a trait. Scala allows one superclass and several mixed-in traits. Required abstract members let a mixin depend on host capabilities without fixing one concrete superclass; member conflicts and super calls are resolved using the language's linearization rules.
A Scala trait can intercept a method, call the next implementation through super, and add behavior before or after it. abstract override marks such an implementation as requiring an appropriate concrete method later in the class linearization. A character-counting plugin can update its cached counts only after a successful underlying document edit, preserving its additional representation invariant without changing the edit's abstract result.
A counting trait that increments the original argument to insertion assumes that subsequent traits store that argument unchanged. A later capitalization trait breaks the assumption: the counter can record lowercase a while the document stores uppercase A. To maintain counts of actual document contents across such a composition, inspect the stored character after delegation or constrain the legal mixin order. Correctness for one base implementation does not establish correctness under every trait combination.

Articles by others on the same topic (0)

There are currently no matching articles.