Re: homogenous syntax for type declarations

From: Antonio Menezes Leitao (Antonio.Leitao_at_evaluator.pt)
Date: 08/04/04


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.



Relevant Pages

  • Re: Help writing a new filter
    ... if we just told you the correct syntax for the array declaration you require and you didnt bother to learn about c# syntax then every single c# syntax error you ever got would result in a newsgroup post. ... This would be completely tedious for both you and the newsgroup. ... error, bad array declarator. ... To declare a managed array the rank ...
    (microsoft.public.win32.programmer.directx.video)
  • Re: Variables in SQL-script SQL2005
    ... Not all DCL commands accept variables (see BOL syntax) so you'll need to use dynamic SQL. ... DECLARE @SqlStatement nvarchar; ... N'CREATE LOGIN ' + ... DECLARE @brugernavn as char ...
    (comp.databases.ms-sqlserver)
  • Re: Syntax error
    ... syntax check on your original post and got: ... >> You should always declare all variables as lexically scoped in the ... Lexically scoped variables are ones declared using my. ... means it has effect until to the end of the current source code block ...
    (comp.lang.perl.misc)
  • Re: Syntax via macro characters.
    ... So I would like to have the syntax interpreted ... (funcall *typed-expander* expr type))) ... (declare (ignore dash)) ... ;; macro definition goes here. ...
    (comp.lang.lisp)
  • Re: Simple question on Pointers
    ... It is undefined to use plain old scalar delete on ... hacks) You can't declare a charEVER. ... As for the inability to declare a variable with that syntax, ... without having to introduce a typedef to insert the variable names before ...
    (microsoft.public.vc.language)