kellysmith12.21
2020-9-26 11:03:07

Is it idiomatic to define an “abstract struct” that provides a interface, then define struct as subtypes of it?


samth
2020-9-26 13:43:24

For incrementally building strings, string output ports are the usual solution.


chansey97
2020-9-26 14:14:38

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).


samth
2020-9-26 14:16:18

Yes, that’s correct


chansey97
2020-9-26 14:16:38

Thanks.


kellysmith12.21
2020-9-26 16:14:20

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?


kellysmith12.21
2020-9-26 16:16:30

I have tried to do this, but it appears that calling the method does not dispatch to the implementation for A and instead recurses.


cris2000.espinoza677
2020-9-26 16:23:13

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


kellysmith12.21
2020-9-26 22:46:40

Hm, that won’t work in my case… I accidentally oversimplified the problem. In fact, the method implementations are mutually recursive.


cris2000.espinoza677
2020-9-26 22:48:01

hmph mind giving a gist with a more fleshed out example of the problem?


rokitna
2020-9-26 22:48:05

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


kellysmith12.21
2020-9-26 23:04:08

That works! Thank-you.


kellysmith12.21
2020-9-26 23:04:51

I tried @rokitna’s suggestion, and that solved my problem.