What does with-compilation-unit do?

Discussion of Common Lisp
Post Reply
psismondi
Posts: 13
Joined: Mon Jan 11, 2010 7:48 pm

What does with-compilation-unit do?

Post by psismondi » Wed Feb 17, 2010 7:56 am

Greetings.

I am reading through some interesting example code that uses WITH-COMPILATION-UNIT. I have read the hyperspec definition of this, but it does not really mean anything to me. Nor do the various CL textbooks I have say anything about it.

Can someone give a an example/explanation of why one might use WITH-COMPILATION-UNIT?

Best,

- Phil -

ramarren
Posts: 613
Joined: Sun Jun 29, 2008 4:02 am
Location: Warsaw, Poland
Contact:

Re: What does with-compilation-unit do?

Post by ramarren » Wed Feb 17, 2010 8:16 am

The primary example of WITH-COMPILATION-UNIT is ASDF. The most important use is the one given as example in Hyperspec: to suppress warnings about undefined functions which will be defined later. One normally wouldn't use WITH-COMPILATION-UNIT unless creating a build system.

Implementations are also free to extend the functionality, for example SBCL allows attaching information to definitions inside a compilation unit available for introspection, which is used by Swank (CL-side component of Slime).

psismondi
Posts: 13
Joined: Mon Jan 11, 2010 7:48 pm

Re: What does with-compilation-unit do?

Post by psismondi » Wed Feb 17, 2010 2:23 pm

OK, thanks. So it appears, from my experiments, that if one uses WITH-COMPILATION-UNIT and a function is referred to first, but defined later withing the same "unit", no warnings at all are issued.

Is that the intention? Just to cut down on system build noise? Or does it *do* something more?

ramarren
Posts: 613
Joined: Sun Jun 29, 2008 4:02 am
Location: Warsaw, Poland
Contact:

Re: What does with-compilation-unit do?

Post by ramarren » Wed Feb 17, 2010 2:57 pm

I am no expert on Common Lisp compilation model, but the spec says "Within the dynamic environment of with-compilation-unit, actions deferred by the compiler until the end of compilation will be deferred until the end of the outermost call to with-compilation-unit. ", and as far as I know there is nothing actually required by the specification to be deferred. So, it is not required to do anything by the standard, but, implementations are free to extend this.

Therefore it does not make sense to ask what it does in the abstract. It is implementation specific. For example, it could be in principle used to signify block compilation, but I don't think there is an implementation that does that.

There is an issue writeup here. The warnings thing appears to be a primary motivation. Other things have been considered (global proclamation scope for example), but they were not included in the standard.

psismondi
Posts: 13
Joined: Mon Jan 11, 2010 7:48 pm

Re: What does with-compilation-unit do?

Post by psismondi » Fri Feb 19, 2010 8:01 am

Thanks, Ramarren. The issue write-up is very helpful. I certainly get the drift of what it is intended to do now.

Even an implementation-dependent part of the ANSI spec is intended to accomplish something that was in people's head at the time the spec was written. That's what I was puzzled about. I have spent much time digging around in Clozure CL source code to find out how things are implemented, but sometimes I just dont' "get it".

The write-up answered the question well.

Thanks again.

Post Reply