Is it idiomatic to define an “abstract struct” that provides a interface, then define struct as subtypes of it?
For incrementally building strings, string output ports are the usual solution.
Can I think that the second form of the begin
(https://docs.racket-lang.org/reference/begin.html) is semantically equivalent to a function call? > (begin expr …+) That is (define (beginf . exps)
(if (null? exps)
(void)
(last exps)))
(+ 1 (beginf (displayln "111")
(displayln "222")
333))
;;111
;;222
;;334
= (+ 1 (begin (displayln "111")
(displayln "222")
333))
;;111
;;222
;;334
Because in Racket (https://docs.racket-lang.org/guide/application.html) > a function call is evaluated by first evaluating the proc-expr and all arg-exprs in order (left to right).
Yes, that’s correct
Thanks.
Say that I have structure types A
and B
that implement a generic interface. Is it possible to have a method implementation for type B
call the implementation for type A
?
I have tried to do this, but it appears that calling the method does not dispatch to the implementation for A
and instead recurses.
iirc for generics you define like #:methods gen-id
[(define proc-id proc-expr)]
well you could have a proc like (define (A-impl self . args)
(void))
and in the struct A #:methods gen-id
[(define proc-id A-impl)]
so then in B #:methods gen-id
[(define (proc-id b . args)
(apply A-impl b args))]
now i dont really remember if this is alright, also this is the naive approach
Hm, that won’t work in my case… I accidentally oversimplified the problem. In fact, the method implementations are mutually recursive.
hmph mind giving a gist with a more fleshed out example of the problem?
Yeah, inside the place where you define the B
method, any call to the method will just directly call the B
method you defined instead of dispatching on the value. The way around that is to define another function with a different name that’s an alias of the dispatching version, and then you can call that one. The define/generic
utility is a recommended way to define that alias, and its documentation has a full example: https://docs.racket-lang.org/reference/struct-generics.html?q=define-generic#%28form._%28%28lib._racket%2Fgeneric..rkt%29._define%2Fgeneric%29%29\|https://docs.racket-lang.org/reference/struct-generics.html?q=define-generic#%28form._%28%28lib._racket%2Fgeneric..rkt%29._define%2Fgeneric%29%29
That works! Thank-you.
I tried @rokitna’s suggestion, and that solved my problem.