Previous: , Up: iklib records defs   [Index]


6.19.1.13 Implemented interfaces

In a use of the syntax define-record-type, the definition clause:

(implements ?interface-name ...)

causes the listed interface names to be included in the list of interfaces implemented by this record–type’s, (vicare-typed)Interface types. The clause implements is accepted by define-record-type when the typed language is selected.

A record–type implements an interface when it implements a method for each method prototype included in the interface–type definition; the record–type can miss the implementation of interface–type methods having a default implementation. A record–type method matches an interface–type method prototype when: record–type method’s type signature is a sub–type of the method prototype.

The implements clause causes a validation of the record–type’s implemented methods against the interface’s method prototypes: if a mismatch occurs, or a method is missing, an exception is raised at expand–time.

Here is a simple example in which the record–type <blue> implements the interface <IOne>; the function fun can be applied to any object whose type implements the interface <IOne>:

(define-interface-type <IOne>
  (method-prototype ione-doit
    (lambda () => (<number>))))

(define-record-type <blue>
  (implements <IOne>)
  (fields val)
  (method ({ione-doit <number>})
    (+ 10 (.val this))))

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

(fun (new <blue> 1)))
⇒ 11

Another example in which the record–type <duo> implements the interface–type <Stringer> which has a method ‘to-string’ with default implementation:

(define-interface-type <Stringer>
  (method (to-string)
    (with-output-to-string
      (lambda ()
        (display this)))))

(define-record-type <duo>
  (implements <Stringer>)
  (fields one two)
  (custom-printer
    (lambda ({this <duo>} port sub-printer)
      (display "#[duo "    port)
      (display (.one this) port)
      (display #\space     port)
      (display (.two this) port)
      (display #\]         port))))

(define (fun {O <Stringer>})
  (.to-string O))

(fun (new <duo> 1 2)))
⇒ "#[duo 1 2]"

Previous: , Up: iklib records defs   [Index]