[plt-scheme] define-struct vs make-struct-type: struct type descriptor

From: Chongkai Zhu (czhu at cs.utah.edu)
Date: Wed Jun 4 17:36:54 EDT 2008

If you read the doc of v372:

A define-struct expression with n fields defines 4 + 2n names:

* struct:s, a structure type descriptor value that represents the new 
datatype. This value is rarely used directly.

* make-s, a constructor procedure that takes n arguments and returns a 
new structure value.

* s?, a predicate procedure that returns #t for a value constructed by 
make-s (or the constructor for a subtype; see section 4.2) and #f for 
any other value.

* s-field, for each field, an accessor procedure that takes a structure 
value and extracts the value for field.

* set-s-field!, for each field, a mutator procedure that takes a 
structure and a new field value. The field value in the structure is 
destructively updated with the new value, and void is returned.

* s, a syntax binding that encapsulates information about the structure 
type declaration. This binding is used to define subtypes (see 
section 4.2). It also works with the shared and match forms (see 
Chapter 44 and Chapter 27 in PLT MzLib: Libraries Manual). For detailed 
information about the expansion-time information stored in s, see 
section 12.6.4.


So "(define-struct foo (bar))" defines the name foo. While 
make-struct-type doesn't do the syntax binding.


Michael Forster wrote:
> Hi,
> I'm using v372, and I've defined tuple structure type as shown below.
> I also want to define a constructor, foo, supplementary to make-foo
> as make-list is to list.
> #lang mzscheme
> (define-struct foo (bar))
> (define foo (lambda (bar) (make-foo bar)))
> When I run this, I get the following:
> Welcome to DrScheme, version 372 [3m].
> Language: Textual (MzScheme, includes R5RS).
> module: duplicate definition for identifier in: foo
> If I use make-struct-type instead, following the style illustrated
> in the Help Desk, as shown below, I don't get the collision
> between identitiers.
> #lang mzscheme
> (define-values (struct:foo
>                 make-foo
>                 foo?
>                 foo-ref
>                 foo-set!)
>   (make-struct-type 'foo #f 1 0))
> (define foo (lambda (bar) (make-foo bar)))
> My interpretation of the v372 Help Desk reference for define-struct
> and make-struct-type is that these should be equivalent.  If so, why
> does define-struct result in the definition of a foo identifier, while
> make-struct-type does not?
> Thanks,
> Mike
> _________________________________________________
>   For list-related administrative tasks:
>   http://list.cs.brown.edu/mailman/listinfo/plt-scheme

Posted on the users mailing list.