6.6 Traits (original) (raw)
6.6 Traits🔗ℹ
A trait is a collection of methods that can be converted to a mixin and then applied to a class. Before a trait is converted to a mixin, the methods of a trait can be individually renamed, and multiple traits can be merged to form a new trait.
Creates a trait. The body of a trait form is similar to the body of a class* form, but restricted to non-private method definitions. In particular, the grammar ofmaybe-renamed, method-definition, andfield-declaration are the same as for class*, and every method-definition must have a corresponding declaration (one of public, override, etc.). As inclass, uses of method names in direct calls, supercalls, and inner calls depend on bringing method names into scope via inherit, inherit/super,inherit/inner, and other method declarations in the same trait; an exception, compared to class is thatoverment binds a method name only in the corresponding method, and not in other methods of the same trait. Finally, macros such as public* and define/public work intrait as in class.
External identifiers in trait, trait-exclude,trait-exclude-field, trait-alias,trait-rename, and trait-rename-field forms are subject to binding via define-member-name anddefine-local-member-name. Although private methods or fields are not allowed in a trait form, they can be simulated by using a public or field declaration and a name whose scope is limited to the trait form.
Returns #t if v is a trait, #f otherwise.
Converts a trait to a mixin, which can be applied to aclass to produce a new class. An expression of the form
is equivalent to
Normally, however, a trait’s methods are changed and combined with other traits before converting to a mixin.
(trait-sum tr ...+) → trait? tr : trait?
Produces a trait that combines all of the methods of the giventrs. For example,
creates a trait t3 that is equivalent to
but t1 and t2 can still be used individually or combined with other traits.
When traits are combined with trait-sum, the combination drops inherit, inherit/super,inherit/inner, and inherit-field declarations when a definition is supplied for the same method or field name by another trait. The trait-sum operation fails (theexn:fail:contract exception is raised) if any of the traits to combine define a method or field with the same name, or if an inherit/super orinherit/inner declaration to be dropped is inconsistent with the supplied definition. In other words, declaring a method withinherit, inherit/super, or inherit/inner, does not count as defining the method; at the same time, for example, a trait that contains an inherit/super declaration for a method m cannot be combined with a trait that definesm as augment, since no class could satisfy the requirements of both augment and inherit/super when the trait is later converted to a mixin and applied to a class.
(trait-exclude trait-expr id)
Produces a new trait that is like the trait result oftrait-expr, but with the definition of a method named byid removed; as the method definition is removed, either aninherit, inherit/super, or inherit/innerdeclaration is added:
- A method declared with public, pubment, orpublic-final is replaced with an inheritdeclaration.
- A method declared with override or override-finalis replaced with an inherit/super declaration.
- A method declared with augment, augride, oraugment-final is replaced with an inherit/inner declaration.
- A method declared with overment is not replaced with any inherit declaration.
If the trait produced by trait-expr has no method definition forid, the exn:fail:contract exception is raised.
(trait-exclude-field trait-expr id)
Produces a new trait that is like the trait result oftrait-expr, but with the definition of a field named byid removed; as the field definition is removed, aninherit-field declaration is added.
(trait-alias trait-expr id new-id)
Produces a new trait that is like the trait result oftrait-expr, but the definition and declaration of the method named by id is duplicated with the name new-id. The consistency requirements for the resulting trait are the same as fortrait-sum, otherwise the exn:fail:contract exception is raised. This operation does not rename any other use of id, such as in method calls (even method calls to identifier in the cloned definition for new-id).
(trait-rename trait-expr id new-id)
Produces a new trait that is like the trait result oftrait-expr, but all definitions and references to methods named id are replaced by definitions and references to methods named by new-id. The consistency requirements for the resulting trait are the same as for trait-sum, otherwise theexn:fail:contract exception is raised.
(trait-rename-field trait-expr id new-id)
Produces a new trait that is like the trait result oftrait-expr, but all definitions and references to fields named id are replaced by definitions and references to fields named by new-id. The consistency requirements for the resulting trait are the same as for trait-sum, otherwise theexn:fail:contract exception is raised.