I’m tying to work with traits and I discovered that traits can’t implement interfaces, so I can’t use the is-a? predicate.
There’s a solution to this problem: create an empty mixin that adds only the interface definition.
Here’s an example:
(require racket/trait) (define simple-int (interface () method1)) (define simple% (class* object% () (super-new))) (define simple-trait (trait (define/public (method1) 0))) (define simple-class% ((trait->mixin simple-trait) simple%)) (define simple-mixin/int (mixin () (simple-int) (super-new))) (define new-class% (simple-mixin/int simple-class%)) (define new-object (new new-class%)) (is-a? new-object simple-int) => #t
I tried to write this macro, but it doesn’t work.
(define-syntax trait->mixin* (syntax-rules () [((trait->mixin* T (interface-exp …)) C) (begin (define mixin/int (mixin () (interface-exp …) (super-new))) (mixin/int ((trait->mixin T) C)))]))
What do you think?