I'm writing my first typed/racket application which is a cli application which asks you some statistical questions about your day such as how many hours you have slept the previous night, how much you have eaten, how many cups of coffee you had etc. The application does this by first displaying the question and then waiting for user input via the function read
. The gathered information is then put into a struct
with predefined fields.
The struct
looks like this:
(struct: day-stats
([user-id : Integer]
;; ... 10 more Integer fields
))
My approach for this is to first initialize the struct with a default value of 0 for each field and then updating that "instance" of the struct with information as the user inputs it, using the struct-copy
function.
That leads me to the question, how do I initialize struct with default values in the most elegant way? Do I create a macro which creates code during compilation/parsing which looks something like this:
(day-stats 0 0 0 0 0 0 0 0 0 0 0)
Or is there already a macro which can do this by reading some kind of property in the struct?
EDIT:
After consulting the documentation some more I found out that there are a property you can set in a struct to auto set fields. However I can't find if this is possible in typed/racket
. Here's what it looks like in racket
:
(struct day-stats
([user-id #:auto]
;; ... 10 more Integer fields
)
#:auto-value 0)
Structs that use
#:auto
aren't supported in Typed Racket currently. In general,#:auto
is a pretty inflexible feature, which I wouldn't use normally, either in Racket or in Typed Racket.Instead, just create a simple constructor function, like this:
This generalizes easily to lots more fields. You can hide the regular constructor by controlling what you
provide
from your module.