declaring macros returning type

Discussion of Common Lisp
Post Reply
lrodrig
Posts: 17
Joined: Thu Sep 16, 2010 4:52 pm

declaring macros returning type

Post by lrodrig » Tue Aug 16, 2011 12:43 am

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.

edgar-rft
Posts: 226
Joined: Fri Aug 06, 2010 6:34 am
Location: Germany

Re: declaring macros returning type

Post by edgar-rft » Tue Aug 16, 2011 1:04 am

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

lrodrig
Posts: 17
Joined: Thu Sep 16, 2010 4:52 pm

Re: declaring macros returning type

Post by lrodrig » Tue Aug 16, 2011 1:39 am

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.

edgar-rft
Posts: 226
Joined: Fri Aug 06, 2010 6:34 am
Location: Germany

Re: declaring macros returning type

Post by edgar-rft » Tue Aug 16, 2011 2:35 am

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

Post Reply