[plt-scheme] creating an instance of a structure

From: Eli Barzilay (eli at barzilay.org)
Date: Sat Jul 13 14:37:41 EDT 2002

(This is a second resend, I don't know if it's just me or not, but it
seems like the list drops almost every second mail I send it.)

On Jul 12, Alex Peake wrote:
> 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.