September 7th, 2012


миксины в racket

Scheme with Classes, Mixins, and Traits

Since class is an expression form instead of a top-level declaration as in Smalltalk and Java, a class form can be nested inside any lexical scope, including lambda. The result is a mixin, i.e., a class extension that is parameterized with respect to its superclass. For example, we can parameterize the picky-fish% class over its superclass:

(define (picky-mixin %)
  (class % (super-new)
    (define/override (grow amt) (super grow (∗ 3/4 amt)))))

(define picky-fish% (picky-mixin fish%))
В этом примере % это параметр функции picky-mixin. Параметр типа "класс". В теле функции макрос class на основе параметра создает новый класс с подмешанной функциональностью. Такие фокусы в динамических языках мне нравятся.