Re: [racket-users] [racket users] Generics question

2020-03-03 Thread Kevin Forchione



> On Mar 3, 2020, at 10:48 AM, Jon Zeppieri  wrote:
> 
> On Tue, Mar 3, 2020 at 12:37 PM Kevin Forchione  wrote:
>> 
>> Thanks!  That brings me a little closer in appreciating the comments I’ve 
>> read about replacing object-oriented code with structs and methods.
>> 
>> Is this part of the racket/generic or the Multimethods library? The example 
>> you provide works from racket/generic, but the search in docs pulls up only 
>> the multimethod docs and the examples don’t work from racket/generic.
>> 
>> Kevin
> 
> It's in racket/generic. Here's a link to the documentation for
> `define/generic`:
> https://docs.racket-lang.org/reference/struct-generics.html?q=define%2Fgeneric#%28form._%28%28lib._racket%2Fgeneric..rkt%29._define%2Fgeneric%29%29
> 
> - Jon

Oh, I see. I searched for “define-generic” instead of “define/generic”! Well, 
that explains my confusion and opens up more options as well. :) 

Kevin

-- 
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/5FFB3F9E-0C68-4F6A-B36A-41D496F22103%40gmail.com.


Re: [racket-users] [racket users] Generics question

2020-03-03 Thread Jon Zeppieri
On Tue, Mar 3, 2020 at 12:37 PM Kevin Forchione  wrote:
>
> Thanks!  That brings me a little closer in appreciating the comments I’ve 
> read about replacing object-oriented code with structs and methods.
>
> Is this part of the racket/generic or the Multimethods library? The example 
> you provide works from racket/generic, but the search in docs pulls up only 
> the multimethod docs and the examples don’t work from racket/generic.
>
> Kevin

It's in racket/generic. Here's a link to the documentation for
`define/generic`:
https://docs.racket-lang.org/reference/struct-generics.html?q=define%2Fgeneric#%28form._%28%28lib._racket%2Fgeneric..rkt%29._define%2Fgeneric%29%29

- Jon

-- 
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/CAKfDxxxfSLiqwYof4DaanadssHMTtR2QTJk2Q1nNRzGssoM1zw%40mail.gmail.com.


Re: [racket-users] [racket users] Generics question

2020-03-03 Thread Kevin Forchione


> On Mar 3, 2020, at 9:19 AM, Jon Zeppieri  wrote:
> 
> (struct A (this other) #:transparent
>  #:methods gen:foo
>  [(define/generic generic-foo do-foo)
>   (define (do-foo foo)
> (printf "other=~a ~a"
> (A-this foo)
> (generic-foo (A-other foo])


Thanks!  That brings me a little closer in appreciating the comments I’ve read 
about replacing object-oriented code with structs and methods. 

Is this part of the racket/generic or the Multimethods library? The example you 
provide works from racket/generic, but the search in docs pulls up only the 
multimethod docs and the examples don’t work from racket/generic. 

Kevin

-- 
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/43B656D0-0811-4E15-95FE-8945655AC68A%40gmail.com.


Re: [racket-users] [racket users] Generics question

2020-03-03 Thread Jon Zeppieri
Kevin,

This is what `define/generic` is for. In your example:

On Tue, Mar 3, 2020 at 11:08 AM Kevin Forchione  wrote:
>
> (struct A (this other) #:transparent
>   #:methods gen:foo
>   [(define (do-foo foo)
>  (printf "other=~a ~a"
>  (A-this foo)
>  (do-foo (A-other foo])

The call to `(doo-foo (A-other foo))` is simply a recursive call to
the function that you're in the middle of defining. In order to call
the generic method and have it dispatched appropriately, you need to
use `define/generic`:

(struct A (this other) #:transparent
  #:methods gen:foo
  [(define/generic generic-foo do-foo)
   (define (do-foo foo)
 (printf "other=~a ~a"
 (A-this foo)
 (generic-foo (A-other foo])

- Jon

-- 
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/CAKfDxxzv7S4gnxjZEDT7yD%3Dm8Y4R6t4fCFjFKyixR99r%2Bc15qQ%40mail.gmail.com.


[racket-users] [racket users] Generics question

2020-03-03 Thread Kevin Forchione
Hi guys,
If I create a generic, say foo, for a particular struct and then later 
reference that method on a different type of struct from within the handler 
method I get an error because the call goes to the original structs method and 
not to the method of the other struct. Is there a way to do this?

Here’s a very artificial example:

#lang racket

(require racket/generic)

(define-generics foo
[do-foo foo])

(struct A (this other) #:transparent
  #:methods gen:foo
  [(define (do-foo foo)
 (printf "other=~a ~a"
 (A-this foo)
 (do-foo (A-other foo])
(struct B (lst) #:transparent
  #:methods gen:foo
  [(define (do-foo foo)
 (printf "lst=~a"
 (B-lst foo)))])

(define b (B '(x y z)))
(define a (A 'a b))

(do-foo b) ;=> lst=(x y z)

(do-foo a) ;=> A-this: contract violation
  expected: A?
  given: (B '(x y z))

Thanks, 
Kevin

-- 
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/86CD099C-EC82-4F97-B20A-784933E3C6FC%40gmail.com.