[racket] Some struct questions

From: Adolfo Pérez Álvarez (adolfo.pa at gmail.com)
Date: Sun Jul 15 14:12:50 EDT 2012


Section 4 of the Racket reference says this about `define-struct':

""" Like struct, except that the syntax for supplying a super-id is
different, and a constructor-id that has a make- prefix on id is
implicitly supplied via #:extra-constructor-name.

This form is provided for backwards compatibility; struct is preferred."""

So it seems it is deprecated.

About question 2, in general you don't want to expose the details of
your structures. For example, when defining a queue like:

(struct queue ([front #:mutable] [rear #:mutable]))

you don't want users to set/get the fields directly; instead you
probably want them to use the `enqueue'/`dequeue' functions, which
maintain the abstraction invariants, assuring the queue is consistent
no matter what the users do. In general, I believe it is better not to
provide the setters (unless it really makes sense, of course).

PS: Sorry for the duplicate Harry, I forgot to put
users at racket-lang.org in CC in my original response.

2012/7/15 Harry Spier <vasishtha.spier at gmail.com>:
> 1) In section 8 of the Racket reference there is this example:
> -------------------------------------------
> Examples:
> (define-struct tree (val left right))
>> (match (make-tree 0 (make-tree 1 #f #f) #f)
>     [(tree a (tree b  _ _) _) (list a b)])
> '(0 1)
> ----------------------------------
> but I wasn't able to find any reference in the documentation to a
> constructor form make-struct-id .  I'm assuming that in the above example
> (match (make-tree 0 (make-tree 1 #f #f) #f)
>  is equivalent to
> (match (tree 0 (tree 1 #f #f) #f)
> Is that correct? Is make-struct-id  a deprecated form of the constructor
> procedure struct-id?
> 2) In a module which contains a structure, lets call it  "structure-a" I can
> have the statement:
> (provide (struct-out structure-a))
> which provides the constructor procedure and the getters and setters to
> structure-a
> but I can also have the statement
> (provide structure-a)
> which only provides the constructor function.
> Are there any cases where you would want to have (provide structure-a)
> rather than (provide (struct-out structure-a)) ?
> Thanks,
> Harry Spier
> ____________________
>   Racket Users list:
>   http://lists.racket-lang.org/users

Posted on the users mailing list.