# Traits and interfaces

**URL:** <https://racket.discourse.group/t/traits-and-interfaces/2267>\
**Category:** Questions & Answers\
**Created:** [September 1, 2023, 10:50am UTC](https://racket.discourse.group/t/traits-and-interfaces/2267 "2023-09-01T10:50:01Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![matteo-daddio](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/matteo-daddio/32/1245_2.png) [@matteo-daddio](https://racket.discourse.group/u/matteo-daddio)\
**Post date:** [September 1, 2023, 10:50am UTC](https://racket.discourse.group/t/traits-and-interfaces/2267/1 "2023-09-01T10:50:01Z")

</div>

Hello,  
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?

Matteo

---

<div class="post-metadata">

**Author:** ![SamPhillips](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/samphillips/32/15_2.png) [@SamPhillips](https://racket.discourse.group/u/SamPhillips)\
**Post date:** [September 5, 2023, 4:19pm UTC](https://racket.discourse.group/t/traits-and-interfaces/2267/2 "2023-09-05T16:19:50Z")

</div>

The macro doesn't work because it is trying to match a pattern like:

```scheme
((identifier stuff ...) stuff ...)

```

The Racket macro expander only works with patterns that are either `identifier` or (more commonly):

```scheme
(identifier stuff ...)

```

A working `trait->mixin*` would be something like:

```scheme
(define-syntax trait->mixin*
  (syntax-rules ()
    [(trait->mixin* T (I* ...))
     (let ([T-mixin (trait->mixin T)]
           [I-mixin (mixin () (I* ...) (super-new))])
       (λ (%)
         (I-mixin (T-mixin %))))]))
```
