[plt-scheme] creating an instance of a structure

From: Eli Barzilay (eli at barzilay.org)
Date: Sat Jul 13 03:39:39 EDT 2002

On Jul 12, Alex Peake wrote:
> [Please handle PLT Scheme list administrative tasks through the Web:
>    http://list.cs.brown.edu/mailman/listinfo/plt-scheme]
> This is how I did it in v103:
> (defstruct attr name domain dom-len dom-prec nullable pk-pos fk-pos fk-table fk-attr identity)
> (define attr
>   (lambda (name domain . args)
>     ;;default values
>     (let ((dom-len (getarg args :dom-len #f))
>           [...]
>           (fk-attr (getarg args :fk-attr name))
>           (identity (getarg args :identity #f)))
>       (make-attr name domain dom-len dom-prec nullable pk-pos fk-pos fk-table fk-attr identity))))
> ;; Scans a Lisp-style keyword/value property list.
> (define (getarg initargs name . not-found)
>   [...])

As long as you're using Swindle code, you might as well go with the
whole syntax extension, and use &key.  The equivalent for the above
case (where you require a name but allow overriding it with :name):

  (define (attr name0 domain
           &key (name name0) dom-len [...] (fk-attr name) identity)
    (make-attr ...))

This is part of the basic Swindle syntax stuff that I've managed to
get in v.200 -- but it is still a bit in liquid form...  If anyone is
interested in playing with it, it is the extra.ss file in
http://www.cs.cornell.edu/eli/misc/eli.plt (it adds a lot of other
(good, IMO) stuff as well).

That place has the new version of my HTML-generation thing (which
generates HTML directly, but at some point I'll make it play with the
XML collection), and the reason it is there is that I found keyword
arguments to be *very* useful for writing web stuff (later it was nice
to see that I basically went through the same path described in
http://www.paulgraham.com/lib/paulgraham/bbnexcerpts.txt - search that
page for "keyword").

          ((lambda (x) (x x)) (lambda (x) (x x)))          Eli Barzilay:
                  http://www.barzilay.org/                 Maze is Life!

Posted on the users mailing list.