Page 1 of 1

Can I turn ASSERT off?

Posted: Thu Aug 21, 2008 10:26 am
by humpolec
Is there a mechanism to turn off compilation of the asserts when I want to compile release (optimized) code? Or do I have to redefine ASSERT manually?

Re: Can I turn ASSERT off?

Posted: Thu Aug 21, 2008 12:46 pm
by qbg
If you really want to do this you could use read time conditionals then push the appropriate symbol onto *features* before recompiling. Downside is that you would need to modify any existing code you have to add in the read time conditionals.

Re: Can I turn ASSERT off?

Posted: Thu Aug 21, 2008 4:22 pm
by humpolec
I have no problem doing it myself, I just thought Common Lisp had that functionality - even C has it after all.

Re: Can I turn ASSERT off?

Posted: Thu Aug 21, 2008 4:52 pm
by Paul Donnelly
humpolec wrote:I have no problem doing it myself, I just thought Common Lisp had that functionality - even C has it after all.
Common Lisp already has a way to toggle evaluation of forms, using *features*. What would the benefit of having a specialized mechanism for ASSERT forms be?

Re: Can I turn ASSERT off?

Posted: Thu Aug 21, 2008 8:48 pm
by findinglisp
humpolec wrote:I have no problem doing it myself, I just thought Common Lisp had that functionality - even C has it after all.
Hmmm... this is one of those questions that shows you're still holding onto a C-like worldview. That's nothing to be ashamed of, though. I had the same thing when I first started studying Lisp because we're all products of our prior experience. I'd suggest that before you start worrying about code so optimized that ASSERT is compiled out of it, you instead make sure you are starting to operate in a Lisp worldview. The purpose of ASSERT in Lisp is not quite the same as in C.

Specifically, in C, when an assert fails, the only option is for the program to abort. Typically, ASSERTs are only used for debugging and you want to remove them before you ship. With Lisp's phenomenal integrated debugging capabilities, you wouldn't necessarily want to remove the ASSERTs. They are integrated with the condition/restart system and would either provide you with some great debugging information (stack traces, etc.), or would even allow you to resume program execution after you correct the error. If you don't understand conditions and restarts and such, you should probably spend some time on them before you start worrying about ASSERT.

That said, there are numerous ways to go about doing what you want in Lisp. As people have already pointed out, you can use the conditional reader capabilities. That's the closest thing to C's #ifdef capability. So, you could do:
#+assert (ASSERT foo)
If the "assert" symbol was not included in *FEATURES* at the time the compiler read that form, it would not be included.

You could also create your own macro (e.g. MYASSERT) and used that instead, providing a different expansion when it's time to remove it.

In short, there is at least as much flexibility in Lisp as in C. But before you start exercising that, try to hold your C-like worldview in check for a while and ask yourself (and others), "Why does Lisp do things this way?" Remember that Lisp is even older than C (the second-oldest high-level language). It has a rich history and there are typically reasons why something is done the way it is.

Re: Can I turn ASSERT off?

Posted: Fri Aug 22, 2008 10:55 am
by humpolec
Thanks for the explanation!
I guess some time will pass until my code stops being C-like, then...

Re: Can I turn ASSERT off?

Posted: Fri Aug 22, 2008 7:33 pm
by findinglisp
humpolec wrote:Thanks for the explanation!
I guess some time will pass until my code stops being C-like, then...
Yup, it typically takes a while. At some point, you'll have that "A-ha!" moment when everything will drop into place and you'll realize what makes Lisp so mind-blowingly different. :o At that point, no other programming language will seem good enough. You will have achieved some measure of "smug Lisp weenie-ness". ;)