This is a read-only archive of lispforum.com. The forum was locked to new users and posts and is preserved here as static HTML from a database snapshot taken on 2019-09-07.

declaring macros returning type

4 posts · 1658 views

Hi,

I was wondering if there is something equivalent to ftype for macros. I need to produce a highly optimized piece of code and I'm using macros.

According to CLHS, ftype only works for functions. Is there any way to declare the type a macro will return?

Regards,
Luis.

Re: declaring macros returning type

AFAIK a macro returns no type. A macro returns a piece of Lisp code, which will be evaluated, an then the evaluated Lisp code will return a value of some type. So either the macro must produce the declaration:
(defmacro one ()
  '(let ((number 1))
     (declare (fixnum number))
     number))

(defun return-one ()
  (one))
will expand to:
(defun return-one ()
  (let ((number 1))
    (declare (fixnum number))
    number))
Or use THE if there's no other way:
(defmacro one () '1)

(defun return-one ()
  (the fixnum (one)))
will expand to:
(defun return-one ()
  (the fixnum 1))
HTH - edgar

Re: declaring macros returning type

Hi,

Thank you very much for your answer. You are right when you say that macros return no type. Sorry for my poor explanation. I was referring to the type of the final expression evaluated within the macro. From your examples I'm assuming that including the type declaration inside the macro will do and, therefore, any piece of code calling that macro can get the information about the type from such declaration.

Regards,
Luis.

Re: declaring macros returning type

lrodrig wrote:...Sorry for my poor explanation...
I assumed you know this ;) - I only wanted to make you aware that you were searching at the wrong place.

Usually one uses declarations to speed-up the compiled code at run-time, not to speed-up the compiling of the code. There may be exceptions, if parts of the the code are intended to compile at run-time, e.g. using Lisp for compiling an "embedded" programming language, but that's a very special case.

Macro-expansion happens at compile-time. During macro-expansion types don't matter much because the compiler compiles the expanded macro code, not the macro definition. This means that for run-time speed it's only important that in the expanded macro code all type declarations are at the correct places.

- edgar