18.8 Calling Superclass Methods

 < Free Open Study > 



18.8 Calling Superclass Methods

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 usefulit'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 > 



Types and Programming Languages
Types and Programming Languages
ISBN: 0262162091
EAN: 2147483647
Year: 2002
Pages: 262

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net