For the time being I've decided to use this macro and attach contracts to the 
fallbacks a different way. Please let me know if there's a better solution.

(define-syntax-rule (I/weak/c [method-id method-contract] ...)
(I/c [method-id (or/c #f method-contract)] ...)

On 4/17/21 10:48 PM, Sage Gerard wrote:

> Playing with this program. Its output is in the comments.
>
> ; /tmp/gen.rkt:9:10: assertion violation
> ; expected: a procedure
> ; given: #f
> ; in: method m
> ; (I/c (m (-> I? 1)))
> ; [...removed the rest of the error...]
> ; 1
>
> (module gen-contracts racket/base
> (require racket/contract racket/exn racket/generic)
> (define-generics I [m I] #:fallbacks [(define (m _) 1)])
> (struct noop () #:methods gen:I [])
> (struct impl () #:methods gen:I [(define (m _) 1)])
> (define (try ctor)
> (displayln
> (with-handlers ([exn:fail:contract? exn->string])
> (m (invariant-assertion (I/c [m (-> I? 1)]) (ctor))))))
> (try noop)
> (try impl))
>
> I expected the output to be just two "1" lines, because I assumed the 
> contract combinator from `define-generics` would consider fallback 
> implementations. I do think it makes sense for `I/c` to fail to apply a 
> contract to a method in `noop` because `noop` doesn't implement `m`. But I 
> think the combinator reuses some logic from whatever is making 
> `#:defined-predicate` work, since it represents missing methods as `#f`, and 
> doesn't look at the fallbacks.
>
> But... I wanted a contract that's okay with a missing method if there's a 
> fallback. The only contracts I can write with the `I/c` combinator seem 
> require me to either A) write repetitive method definitions that just parrot 
> the fallback, or B) write its method contracts like `(I/c [m (or/c #f (-> I? 
> 1))])`, which feels icky.
>
> Apologies if I'm totally off the mark or just missed something in the manual, 
> but is there a way to define a contract for a generics implementation that 
> won't complain if it sees a fallback for a missing method?
>
> --
> ~slg
>
> --
> 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.
> To view this discussion on the web visit 
> [https://groups.google.com/d/msgid/racket-users/fed6c561-881b-08af-4318-25750f198133%40sagegerard.com](https://groups.google.com/d/msgid/racket-users/fed6c561-881b-08af-4318-25750f198133%40sagegerard.com?utm_medium=email&utm_source=footer).

--
~slg

-- 
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.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/racket-users/747816e3-44de-9af8-a284-873007c4e57d%40sagegerard.com.

Reply via email to