Re: homogenous syntax for type declarations
From: Antonio Menezes Leitao (Antonio.Leitao_at_evaluator.pt)
Date: 08/04/04
- Next message: Dave Pearson: "Re: Mandelbrot for the terminal"
- Previous message: Antonio Menezes Leitao: "Re: design and lisp"
- In reply to: Bruno Haible: "homogenous syntax for type declarations"
- Next in thread: Bruno Haible: "Re: homogenous syntax for type declarations"
- Reply: Bruno Haible: "Re: homogenous syntax for type declarations"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Wed, 04 Aug 2004 19:23:07 +0100
Bruno Haible <bruno@clisp.org> writes:
> Common Lisp supports typed variables in various situations, but the
> syntax to declare a typed variable is different each time:
>
> - In LAMBDA, LET, LET*, MULTIPLE-VALUE-BIND:
> (lambda (x ...) (declare (integer x)) ...)
> (lambda (x ...) (declare (type integer x)) ...)
> (let ((x ...)) (declare (integer x)) ...)
> (let ((x ...)) (declare (type integer x)) ...)
>
> - In DEFMETHOD, DEFGENERIC:
> (defmethod foo ((x integer) ...) ...)
>
> - In LOOP:
> (loop for x integer across v ...)
> (loop for x of-type integer across v ...)
>
> - Type declarations in function returns:
> (declaim (ftype (function (t) integer) foo))
> (defun foo (x) ...)
>
> A typed variable is a variable whose value at any time is guaranteed
> by the programmer to belong to a given type. It is assumed that the
> type's semantics doesn't change during the dynamic extent of the
> variable.
>
> The new module 'typedvar' in CLOCC [1] supports typed variables and typed
> function returns through a simple common syntax: [variable type] and [type].
> Examples:
>
> - In LAMBDA, LET, LET*, MULTIPLE-VALUE-BIND:
> (lambda ([x integer] ...) ...)
> (let (([x integer] ...) ...)
>
> - In DEFMETHOD, DEFGENERIC:
> (defmethod foo ([x integer] ...) ...)
>
> - In LOOP:
> (loop for [x integer] across v ...)
>
> - Type declarations in function returns:
> (defun foo (x) [integer] ...)
>
> The variable name always comes before the type, because (assuming decent
> coding style) it carries more information than the type.
>
> Example:
> (defun scale-long-float (x exp)
> (declare (long-float x) (fixnum exp))
> ...)
> ->
> (defun scale-long-float ([x long-float] [exp fixnum])
> ...)
>
> Example:
> (labels ((test (df) df))
> (declare (ftype (function (double-float) double-float) test))
> (test 1))
> ->
> (labels ((test ([df double-float]) [double-float] df))
> (test 1))
>
> Note: Specialized lambda lists in DEFMETHOD, DEFGENERIC can contain an
> an evaluated form, whereas type specifiers cannot. Therefore
> (defmethod foo ([x (eql a)]) ...)
> is equivalent to
> (defmethod foo ((x (eql 'a))) ...),
> and there is no typed-variable syntax for
> (defmethod foo ((x (eql a))) ...).
>
> Note: Another difference with specialized lambda lists in DEFMETHOD is
> that the typed variable syntax not only defines a specializer, but
> also
> a declaration for the entire scope of variable. I.e.
> (defmethod foo ([x integer]) ...)
> is equivalent to
> (defmethod foo ((x integer)) (declare (type integer x)) ...)
> It would be bad style anyway to assign a non-integer value to x inside
> the method.
What is the new syntax for the following (old-syntax) method
declaration?
(defmethod foo ((x integer) y)
(declare (integer y))
...)
António Leitão.
- Next message: Dave Pearson: "Re: Mandelbrot for the terminal"
- Previous message: Antonio Menezes Leitao: "Re: design and lisp"
- In reply to: Bruno Haible: "homogenous syntax for type declarations"
- Next in thread: Bruno Haible: "Re: homogenous syntax for type declarations"
- Reply: Bruno Haible: "Re: homogenous syntax for type declarations"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|