| < Free Open Study > |
|
The variable super has been used to copy functionality from superclasses into new subclasses. We can also use super in the bodies of method definitions to extend the superclass's behavior with something extra. Suppose, for instance, that we want a variant of our backupCounter class in which every call to the inc method is automatically preceded by a call to backup. (Goodness knows why such a class would be useful—it's just an example.)
funnyBackupCounterClass = λr:BackupCounterRep. let super = backupCounterClass r in {get = super.get, inc = λ_:Unit. (super.backup unit; super.inc unit), reset = super.reset, backup = super.backup}; ▸ funnyBackupCounterClass : BackupCounterRep → BackupCounter
Note how the calls to super.inc and super.backup in the new definition of inc avoid repeating the superclass's code for inc or backup here. In larger examples, the savings of duplicated functionality in such situations can be substantial.
| < Free Open Study > |
|