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


9 Sharing object–type definition clauses

Mixins are a way to add definition clauses to record–types and labels. Interfaces are implemented half in the boot image and half in the library (vicare language-extensions mixins).

Let’s consider this situation:

(define-record-type <alpha>
  (fields a))

(define-record-type <beta>
  (fields b))

(define-record-type <delta>
  (parent <alpha>)
  (fields v)
  (method (doit)
    (+ 1 (.v this))))

(define-record-type <gamma>
  (parent <beta>)
  (fields v)
  (method (doit)
    (+ 1 (.v this))))

the definitions of <delta> and <gamma> share some clauses; both of them already have a parent type and multiple inheritance is not supported by Vicare. Is it possible to write the shared clauses only once and attach them to the record–type definitions? Yes, with mixins. The example above is equivalent to the following:

(define-record-type <alpha>
  (field a))

(define-record-type <beta>
  (field b))

(define-mixin-type <stuff>
  (field v)
  (method (doit)
    (+ 1 (.v this))))

(define-record-type <delta>
  (parent <alpha>)
  (mixins <stuff>))

(define-record-type <gamma>
  (parent <beta>)
  (mixins <stuff>))

the syntax use of define-mixin-type associates a set of clauses to the identifier <stuff>; when the mixins clause is used in the body of a record–type definition:

  1. The clauses associated to the selected mixin identifier are retrieved as syntax object.
  2. All the instances of the identifier <stuff> are substituted with the identifier of the enclosing record–type; <delta> and <gamma> in the example.
  3. The resulting clauses are added to the enclosing definition.
Auxiliary Syntax: mixins ?mixin-name

Compose the enclosing record–type, label or mixin definition with the given list of mixins. This clause can be used multiple times. This syntactic binding is exported by the library (vicare).

The syntactic identifiers ?mixin-name are used to reference the mixins to be imported. The clauses are included in the same order in which the ?mixin-name identifiers are present in the mixins clause and in the same place in which the mixins clause is present in the enclosing definition.

It is a syntax violation if a mixin identifier name is not already associated to a set of clauses at the time the receiving definition is expanded.

Syntax: define-mixin-type ?mixin-name ?clause0 ?clause ...

Associate a set of mixin clauses to the identifier ?mixin-name, which can be later referenced by a mixins clause in the body of a class or label definition. This syntactic binding is exported by the library (vicare language-extensions mixins).

The mixins clause can be used in the body of a mixin definition; the result is that the clauses of the imported mixins are added to the enclosing mixin definition.

The following clauses are accepted in the body of a define-mixin-type:

define-type-descriptors strip-angular-parentheses
nongenerative sealed opaque protocol super-protocol fields
method virtual-method seal-method
custom-printer type-predicate equality-predicate
comparison-procedure hash-function
public protected private
implements

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