[racket users] struct #:methods question

35 views
Skip to first unread message

Kevin Forchione

unread,
Apr 20, 2020, 6:32:29 PM4/20/20
to Racket-Users List
Hi guys,
How do you return an instance of the structure type from the struct’s #:methods? This is would seem to be a common situation, but it has me stumped.

Kevin

Jon Zeppieri

unread,
Apr 20, 2020, 6:50:31 PM4/20/20
to Kevin Forchione, Racket-Users List
There's no trick to it:

#lang racket/base

(require racket/generic)

(define-generics foo
(foo-do foo x))

(struct thing (x)
#:methods gen:foo
[(define (foo-do f x)
(thing x))])
> --
> 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...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/racket-users/EE404363-995C-4888-8B43-E463EB3C7418%40gmail.com.

David Storrs

unread,
Apr 21, 2020, 4:42:06 PM4/21/20
to Jon Zeppieri, Kevin Forchione, Racket-Users List
On Mon, Apr 20, 2020 at 6:50 PM Jon Zeppieri <zepp...@gmail.com> wrote:
There's no trick to it:

#lang racket/base

(require racket/generic)

(define-generics foo
  (foo-do foo x))

(struct thing (x)
  #:methods gen:foo
  [(define (foo-do f x)
     (thing x))])


On Mon, Apr 20, 2020 at 6:32 PM Kevin Forchione <lys...@gmail.com> wrote:
>
> Hi guys,
> How do you return an instance of the structure type from the struct’s #:methods? This is would seem to be a common situation, but it has me stumped.
>
> Kevin

<self-promotion>

Note that if you're getting fancy with structs then you might want to check out: https://docs.racket-lang.org/struct-plus-plus/index.html

Among other things, it auto-generates functional setters and functional updaters, a keyword constructor, dotted accessors, reflection data, and allows for contracts on individual fields and enforcement of relationships between fields. 

</self-promotion>

>
> --
> 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...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/racket-users/EE404363-995C-4888-8B43-E463EB3C7418%40gmail.com.

--
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...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages