Can I turn ASSERT off?
Can I turn ASSERT off?
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?
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?
I have no problem doing it myself, I just thought Common Lisp had that functionality - even C has it after all.
-
- Posts: 148
- Joined: Wed Jul 30, 2008 11:26 pm
Re: Can I turn ASSERT off?
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?humpolec wrote:I have no problem doing it myself, I just thought Common Lisp had that functionality - even C has it after all.
-
- Posts: 447
- Joined: Sat Jun 28, 2008 7:49 am
- Location: Austin, TX
- Contact:
Re: Can I turn ASSERT off?
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.humpolec wrote:I have no problem doing it myself, I just thought Common Lisp had that functionality - even C has it after all.
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/
Slowly but surely the world is finding Lisp. http://www.findinglisp.com/blog/
Re: Can I turn ASSERT off?
Thanks for the explanation!
I guess some time will pass until my code stops being C-like, then...
I guess some time will pass until my code stops being C-like, then...
-
- Posts: 447
- Joined: Sat Jun 28, 2008 7:49 am
- Location: Austin, TX
- Contact:
Re: Can I turn ASSERT off?
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.humpolec wrote:Thanks for the explanation!
I guess some time will pass until my code stops being C-like, then...


Cheers, Dave
Slowly but surely the world is finding Lisp. http://www.findinglisp.com/blog/
Slowly but surely the world is finding Lisp. http://www.findinglisp.com/blog/