First of all, . won’t work in standard Racket because . has a special
meaning (see
https://docs.racket-lang.org/reference/reader.html#%28part._parse-pair%29).

But yes, this is directly related to the discussion above because with the
field name information, you can write your own accessor.

#lang racket

(module submodule racket
  (provide @)
  (require syntax/parse/define)

  (define (access e fld)
    (match-define-values (info _) (struct-info e))

    ;; for now we hard code it; would be able to extract it from
struct-type-info in Racket 2
    (define fields '(bar baz))

    (match-define-values (name _ _ accessor _ _ _ _) (struct-type-info info))
    (accessor e (index-of fields fld)))

  (define-simple-macro (@ e:expr fld:id) (access e 'fld)))

(require 'submodule)

(struct foo (bar baz) #:transparent)
(define my-foo (foo 1 2))

(@ my-foo baz) ;=> 2

Note that your struct must not be opaque for this to work. Typed Racket
might have enough compile-time data to make this to work with opaque
structs, though.

But for now, https://docs.racket-lang.org/struct-define/index.html might be
a good workaround for your problem.





On Fri, Jun 14, 2019 at 3:37 PM Dmitry Pavlov <dpav...@iaaras.ru> wrote:

> Hello,
>
> While we are at it: is it theoretically possible in Racket or Typed Racket
> (or will be possible in Racket 2 or Typed Racket 2) to access struct fields
> without repeating the name of the struct type again?
>
> Like in C
>
> typedef struct
> {
>    double x;
>    double y;
> } VeryLongStructureName;
>
> VeryLongStructureName a;
>
> double z = a.x + a.y;
>
>
> The analogous Racket code for the last line would be
>
> (define z (+ very-long-structure-name-x a) (+ very-long-structure-name-y
> a))
>
> and I am wondering whether it can be shortened to something like (define z
> (+ (. x a) (. x b)))
>
> (I know that match can work with structs, but it is not always as
> convenient as the C way with the dot)
>
>
> Best regards,
>
> Dmitry
>
>
> --
> 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/d23df714-e808-91f3-b978-29bcca849100%40iaaras.ru
> .
> For more options, visit https://groups.google.com/d/optout.
>

-- 
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/CADcuegsp37-mE_i-wN9k8Q1hAAS%3DyO2fy0_qaQKZs%3DAdxET-vw%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to