0
votes

I am learning Racket at the moment and would like to know if the following is possible out of the box in Racket. When I create an instance of a class I use the following syntax:

(new Client% [name "John"]
             [age  30])

I like the fact that I need to name the field ids when creating an instance of a class. Is there an equivalent when I am creating a struct. I scanned the Racket documentation but could not find anything about naming field ids when creating a struct.

TIA.

1

1 Answers

1
votes

If you look at the documentation, you will find that struct creates a constructor for you that takes the initial values of a structure instance as positional arguments. AFAIK there is no built-in support for a "keyword"-like constructor.

Having said that, you can always do something like

(struct client (name age))
(struct client (name age) #:transparent)
(define (new-client #:name n #:age a)
  (client n a))
(new-client #:age 20 #:name "me")

to get the usual function with keyword feeling.