Would anyone be willing to give some advice on how #:fallbacks is intended to be used? It seems someone else was similarly confused, but nobody answered: https://stackoverflow.com/questions/52287226/why-cant-code-in-fallbacks-refer-to-the-generic-methods
The issue is attempting to refer to methods defined in #:defaults when defining a #:fallback. I was attempting to add a default method to Lexi Lamba’s generic collections library: (define-generics indexable
(ref indexable . _)
(set-ref indexable . _)
(update-ref indexable . _)
#:defaults
([hash? (define ref hash-ref)
(define set-ref hash-set)
(define update-ref hash-update)]
[dict? (define ref dict-ref)
(define set-ref dict-set)
(define update-ref dict-update)]
[sequence? (define ref nth)
(define set-ref set-nth)]
)
#:fallbacks
[(define/generic reference ref)
(define/generic set-reference set-ref)
(define (update-ref indexable index proc)
(let ([old (reference indexable index)])
(set-reference indexable index (proc old))))])
but without the extra (define/generic …) forms, it claims ref and set-ref are not defined. Surely this isn’t how it’s supposed to be done (the linked OP mentions it is in fact a syntax error to use (define/generic) outside of #:methods), but I’d argue that it’s not immediately obvious how this works.
Cheers.
As I understand within the #:methods
the interface names (ref/set-ref/update-ref) are from the scope of the #:methods
block. So within the block a function call doesn’t have to go through the generics machinery, but if you want to go through the machinery (like you have wrapped a generic value) you can get to it with define/generic
Edit: indeed, it is mentioned in the documentation for define/generics: “When used inside the method definitions associated with the #:methods keyword, binds local-id to the generic for method-id. This form is useful for method specializations to use generic methods (as opposed to the local specialization) on other values.” in fairness, the documentation on #:fallbacks mentions it has the exact same form as #:methods. Honestly, though, I don’t think I’d have had any chance of coming across/working that out for myself. Oh well, now I know! Thank you :slightly_smiling_face:
@awz.ang has left the channel