> On Jun 25, 2017, at 12:25 PM, Matthew Butterick <m...@mbtype.com> wrote:
> 
> How does one attach a generic interface to a class?
> 
> `class*` allows one to attach an `interface` [2] (in the class sense of that 
> term, which I understand is separate from that of "generic interface"). 
> 
> Then the docs for `interface*` show an example attaching the 
> `prop:custom-write` property. [3] But the docs for `prop:custom-write` say 
> that it's deprecated, and one should "use `gen:custom-write`, instead" [4] 
> (that is, the generic-interface version this concept).
> 
> Yet I when I put `gen:custom-write` into `interface*`, it triggers an 
> "illegal use of syntax" error.
> 
> #lang racket
> 
> (define i<%> (interface* () ([gen:custom-write
>                               (lambda (obj port mode) (void))])
>                method1 method2 method3))
> 
> So I'm not clear whether I'm doing it wrong, or if `interface*` only works 
> with the (deprecated) property-based technique.

It's possible to do that with the private internal macros in 
"racket/private/generic-methods.rkt". If you don't mind using something that's 
supposed to be private and unstable, and if you don't mind things breaking if 
anything ever changes, you can use `generic-property` and 
`generic-method-table` like this:

#lang racket
(require racket/private/generic-methods)

(define i<%>
  (interface* ()
              ([(generic-property gen:custom-write)
                (generic-method-table gen:custom-write
                  (define (write-proc obj port mode)
                    (void)))])))

However, it might be better for someone to make a pull request to update the 
`interface*` form to allow generic interfaces properly without the user needing 
these internal macros.

Alex Knauth

> [2] 
> http://docs.racket-lang.org/reference/createinterface.html?q=class*#%28form._%28%28lib._racket%2Fprivate%2Fclass-internal..rkt%29._interface%29%29
>  
> <http://docs.racket-lang.org/reference/createinterface.html?q=class*#(form._((lib._racket/private/class-internal..rkt)._interface))>
> 
> [3] 
> http://docs.racket-lang.org/reference/createinterface.html?q=class*#%28form._%28%28lib._racket%2Fprivate%2Fclass-internal..rkt%29._interface%2A%29%29
>  
> <http://docs.racket-lang.org/reference/createinterface.html?q=class*#(form._((lib._racket/private/class-internal..rkt)._interface*))>
> 
> [4] 
> http://docs.racket-lang.org/reference/Printer_Extension.html?q=class*#%28def._%28%28quote._~23~25kernel%29._prop~3acustom-write%29%29
>  
> <http://docs.racket-lang.org/reference/Printer_Extension.html?q=class*#(def._((quote._~23~25kernel)._prop~3acustom-write))>

-- 
You received this message because you are subscribed to the Google Groups 
"Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to racket-users+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to