Next: , Previous: , Up: methods   [Contents][Index]


7.3 Sealing methods

It happens that we want to define virtual methods in a super–type, override them in a sub–type and then forbid further overriding in the sub–types of the sub-type. This is possible with sealing methods.

The clause seal-method allows the definition of methods associated to a record–type that might override the super–type’s methods, but that forbid the sub–types from overriding them. For everything else: sealing methods work like concrete methods.

In the following example everything works as usual, with <sub> overriding the implementation of doit in <super>:

(define-record-type <super>
  (virtual-method (doit)
    1))

(define-record-type <sub>
  (parent <super>)
  (seal-method (doit)
    2))

(define (super-fun {O <super>})
  (.doit O))

(define (sub-fun   {O <sub>})
  (.doit O))

(define O
  (new <sub>))

(super-fun O)   ⇒ 2
(sub-fun   O)   ⇒ 2

but adding the following definition will cause an expand–time exception, because sub–types of <sub> are forbidden from having a method named doit:

(define-record-type <sub-sub>
  (parent <sub>)
  (method (doit)
    3))
error→ &syntax

the following definition will also cause an expand–time exception, because sub–types of <sub> are forbidden from having a field named doit:

(define-record-type <sub-sub>
  (parent <sub>)
  (fields doit))
error→ &syntax