Subject: Re: A better syntax for type declaration?
From: rpw3@rpw3.org (Rob Warnock)
Date: Fri, 01 Dec 2006 21:32:52 -0600
Newsgroups: comp.lang.lisp
Message-ID: <7JGdnQV_qf95bu3YnZ2dnUVZ_qGdnZ2d@speakeasy.net>
Pillsy <pillsbury@gmail.com> wrote:
+---------------
| Pascal Costanza wrote:
| > (c) It should be relatively straightforward to implement what you
| > suggest as a bunch of macros that expand into the CL-style declarations.
| 
| "Relatively straightforward" is right, since I've been able to whip a
| potentially functional implementation up in a few hours. The trickiest
| part was parsing the lambda list for &key, &aux and friends, and I may
| have completely botched the job.
+---------------

You might want to look at what, if any, implementation-specific
functions you have available to you that you can piggyback on
[and then if you're interested in portability, provide feature-tested
wrappers around the particular functionality you need]. For example,
CMUCL provides the function KERNEL:PARSE-LAMBDA-LIST, which does this:

    ;;; Parse-Lambda-List  --  Interface
    ;;;
    ;;;  Break a lambda-list into its component parts.
    ;;;  We return eleven values:
    ;;;  1] A list of the required args.
    ;;;  2] A list of the optional arg specs.
    ;;;  3] True if a rest arg was specified.
    ;;;  4] The rest arg.
    ;;;  5] A boolean indicating whether keywords args are present.
    ;;;  6] A list of the keyword arg specs.
    ;;;  7] True if &allow-other-keys was specified.
    ;;;  8] A list of the &aux specifiers.
    ;;;  9] True if a more arg was specified.
    ;;; 10] The &more context var
    ;;; 11] The &more count var
    ;;;
    ;;; The top-level lambda-list syntax is checked for validity,
    ;;; but the arg specifiers are just passed through untouched.
    ;;; If something is wrong, we use Compiler-Error, aborting
    ;;; compilation to the last recovery point.
    ;;;
    (defun parse-lambda-list (list)
      (declare (list list)
	       (values list list boolean t boolean list boolean list
		       boolean t t))
      ...[rest of function]... )

For example:

    cmu> (kernel:parse-lambda-list
	  '(a b &optional c (d 37) (e (+ d 12) e-p)
		&rest the-rest
		&key f (g 32) (h (/ g 1.2) h-p)
		&allow-other-keys
		&aux i (j (+ d (* e g)))))

    (A B)
    (C (D 37) (E (+ D 12) E-P))
    T
    THE-REST
    T
    (F (G 32) (H (/ G 1.2) H-P))
    T
    (I (J (+ D (* E G))))
    NIL
    NIL
    NIL
    cmu> 

I'm sure most other implementations have something similar.


-Rob

-----
Rob Warnock			<rpw3@rpw3.org>
627 26th Avenue			<URL:http://rpw3.org/>
San Mateo, CA 94403		(650)572-2607