Access to structure subtype strings in Schema and Racket

In Racket, this gives me an error:

(struct point-in-plane (pos_x pos_y)) (struct pixel point-in-plane (color)) (define a-pixel (pixel 1 2 "blue")) (pixel-color a-pixel) (pixel-pos_x a-pixel) (pixel-pos_y a-pixel) 

To do this, I need to replace the last two lines:

 (point-in-plane-pos_x a-pixel) (point-in-plane-pos_y a-pixel) 

Similarly in R6RS

 #!r6rs (import (rnrs)) (define-record-type point (fields xy)) (define-record-type cpoint (parent point) (fields color)) (define blue-point (make-cpoint 1 2 "blue")) (write (cpoint-x blue-point)) 

Gives a similar error.

What is the reason why Scheme (and Racket) does not allow you to access the subtype fields that were defined in the parent element: subtypeID-fieldID instead of parenttypeID-fieldID

those. in my case, letting me use pixel-pos_x and pixel-pos_y.

+4
source share
2 answers

One of the reasons is that struct allows you to define substructures with the same named fields. For instance:

 (struct base (xy)) (struct sub base (xy)) (define rec (sub 1 2 3 4)) (base-x rec) ; => 1 (sub-x rec) ; => 3 

This is because structures do not know the names of the fields. From Racquet Documentation : "Fields of type structure are essentially unnamed, although names are supported for error reporting purposes." You will need to disable this behavior in order to get additional accessors for substructures.

+7
source

the documentation in the form of a structure says that it provides accessors and setters for the given fields, but does not say that it will automatically re-imagine the existing accessors and setters of the parent type with the additional names that you expect.

When I am structuring and pulling components by name, I often use the racket / match library, especially with struct * pattern matching. I usually have to deal with several components of the structure, and this allows me to do this.

+5
source

Source: https://habr.com/ru/post/1387799/


All Articles