Can I turn ASSERT off?

Discussion of Common Lisp

Can I turn ASSERT off?

Postby humpolec » Thu Aug 21, 2008 10:26 am

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?
User avatar
humpolec
 
Posts: 14
Joined: Sun Aug 17, 2008 1:37 pm
Location: Poland

Re: Can I turn ASSERT off?

Postby qbg » Thu Aug 21, 2008 12:46 pm

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.
qbg
 
Posts: 64
Joined: Mon Jun 30, 2008 1:05 pm
Location: Minnesota

Re: Can I turn ASSERT off?

Postby humpolec » Thu Aug 21, 2008 4:22 pm

I have no problem doing it myself, I just thought Common Lisp had that functionality - even C has it after all.
User avatar
humpolec
 
Posts: 14
Joined: Sun Aug 17, 2008 1:37 pm
Location: Poland

Re: Can I turn ASSERT off?

Postby Paul Donnelly » Thu Aug 21, 2008 4:52 pm

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?
Paul Donnelly
 
Posts: 148
Joined: Wed Jul 30, 2008 11:26 pm

Re: Can I turn ASSERT off?

Postby findinglisp » Thu Aug 21, 2008 8:48 pm

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.
Cheers, Dave
Slowly but surely the world is finding Lisp. http://www.findinglisp.com/blog/
findinglisp
 
Posts: 440
Joined: Sat Jun 28, 2008 7:49 am
Location: Austin, TX

Re: Can I turn ASSERT off?

Postby humpolec » Fri Aug 22, 2008 10:55 am

Thanks for the explanation!
I guess some time will pass until my code stops being C-like, then...
User avatar
humpolec
 
Posts: 14
Joined: Sun Aug 17, 2008 1:37 pm
Location: Poland

Re: Can I turn ASSERT off?

Postby findinglisp » Fri Aug 22, 2008 7:33 pm

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". ;)
Cheers, Dave
Slowly but surely the world is finding Lisp. http://www.findinglisp.com/blog/
findinglisp
 
Posts: 440
Joined: Sat Jun 28, 2008 7:49 am
Location: Austin, TX


Return to Common Lisp

Who is online

Users browsing this forum: Yahoo [Bot] and 5 guests