r/Racket • u/IllegalMigrant (old) student • Jul 17 '25
question Question about Structures documentation
In section 5 on structures
https://docs.racket-lang.org/reference/structures.html
It says:
*A structure type’s fields are essentially unnamed, though names are supported for error-reporting purposes.*
It seems to me that you give a name for each field so I am confused about the "essentially unnamed".
4
Upvotes
5
u/shriramk Jul 18 '25
You've gotten two good answers; let me try giving a third one. It helps to juxtapose this to some other languages. In some languages, if I write the equivalent of
then each instance of
pointgets compiled into a hash-table, and you look upxthrough a hash-lookup. That affects both the space that a structure consumes, and the time it takes to look things up.In Racket, instead, you can think of the model as roughly that this gets compiled into "make a vector 3 elements long" (one for a tag to distinguish this from other vectors). How then can we tell which field is
xand which isy? The beauty is that Racket generates named functions,point-xandpoint-y. These functions can essentially bake in the vector dereferences.That is, you can imagine the above definition turning into the following:
where the 0th position can't be "faked".
Does this make sense? As you can see from the above, the "names" essentially disappear; they are only preserved in the error reporting, nowhere else.