The `struct` form is defined in `struct.rkt` [1]. As you can see, `struct`
wraps `define-struct/derived` [2], which uses many things exported from
`struct.c` [3]. The "Inside: Racket C API" doc [5] describes some of these
functions -- see section 16.

On the matter of structs being essentially vectors: Again in `struct.c`
[3], the C function `scheme_make_struct_instance` (line 2388) instantiates
a `Scheme_Structure`, which is declared in `schpriv.h` [4], line 1113. The
field values go into a `slots` array, which points directly to the
`Scheme_Object`s (i.e. arbitrary Racket values) supplied to
`scheme_make_struct_instance`. Assuming Racket vectors are essentially C
arrays, Racket structs are like vectors in this way.

Looking at `Scheme_Struct_Type`, defined just above `Scheme_Structure` in
`schpriv.h` [4], it's clear there's nowhere to store field names. We can
confirm this by looking at `_make_struct_type` in `struct.c` [3], starting
at line 4778, as this is the function used (indirectly) by
`define-struct/derived`.

Eric

[1]
https://github.com/racket/racket/blob/62f5b2c4e4cdefa18fa36275074ff9fe376ddaf3/racket/collects/racket/private/struct.rkt
[2]
https://github.com/racket/racket/blob/62f5b2c4e4cdefa18fa36275074ff9fe376ddaf3/racket/collects/racket/private/define-struct.rkt
[3]
https://github.com/racket/racket/blob/37a985a6812a587b911b677bc094c82a6c76f1c7/racket/src/racket/src/struct.c
[4]
https://github.com/racket/racket/blob/37a985a6812a587b911b677bc094c82a6c76f1c7/racket/src/racket/src/schpriv.h
[5] http://docs.racket-lang.org/inside/index.html

On Mon, Mar 26, 2018 at 9:36 AM Jens Axel Søgaard <jensa...@soegaard.net>
wrote:

> 2018-03-26 17:58 GMT+02:00 Kevin Forchione <lyss...@gmail.com>:
>
>> In another thread on structs it was confirmed that structs are in essence
>> a vector with some fancy bindings associated. But there must be more going
>> on, for instance, the definition (struct foo (A B)) creates a function foo
>> that when applied does not print like a vector. Is there any documentation
>> instructs that does into the details of what they are and are doing under
>> the hood?
>>
>
> FWIW here is my way of implementing structs in JavaScript. I assume the C
> implementation
> of structs is similar.
>
>
> https://github.com/soegaard/urlang/blob/master/compiler-rjs/runtime.rkt#L1730
>
> The most difficult part to implement is structs with supers.
>
> The C implementation is here:
>
> https://github.com/racket/racket/blob/master/racket/src/racket/src/struct.c
>
>
> /Jens Axel
>
> --
> 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.
> 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.
For more options, visit https://groups.google.com/d/optout.

Reply via email to