Page 1 of 1

declaring macros returning type

Posted: Tue Aug 16, 2011 12:43 am
by lrodrig
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

Posted: Tue Aug 16, 2011 1:04 am
by edgar-rft
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:

Code: Select all

(defmacro one ()
  '(let ((number 1))
     (declare (fixnum number))
     number))

(defun return-one ()
  (one))
will expand to:

Code: Select all

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

Code: Select all

(defmacro one () '1)

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

Code: Select all

(defun return-one ()
  (the fixnum 1))
HTH - edgar

Re: declaring macros returning type

Posted: Tue Aug 16, 2011 1:39 am
by lrodrig
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

Posted: Tue Aug 16, 2011 2:35 am
by edgar-rft
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