digitalmars.D.learn - Static asserts within unittest block
- Piotr Szturmaj (3/3) Mar 29 2011 I see this is common practice in Phobos. I though static asserts should
- Jonathan M Davis (22/25) Mar 29 2011 If a static assert is in a unit test block it's to verify that something...
- bearophile (4/11) Mar 29 2011 Right. Some static asserts may cause/need the instantiation of a templat...
- Piotr Szturmaj (1/1) Mar 29 2011 Now it's clear. Thanks to both of you :)
I see this is common practice in Phobos. I though static asserts should be checked at each compilation, not only when compiling with unittest. Or is it supposed to shorten compile time for already tested modules? :)
Mar 29 2011
On 2011-03-29 10:08, Piotr Szturmaj wrote:I see this is common practice in Phobos. I though static asserts should be checked at each compilation, not only when compiling with unittest. Or is it supposed to shorten compile time for already tested modules? :)If a static assert is in a unit test block it's to verify that something works. You don't necessarily want it in normal code. For instance, what if the static assert is verifying something about a templated type or function? Having that static assert in the normal code would mean that that particular instantiation of the function would always be in the code, whereas putting the static assert in the unit test would only result in that instantiation during unit tests (unless you actually instantiated it in your code). Whether static assert is used in normal code or a unit test depends entirely on what's being tested. A common place to use it in normal code would be with version blocks. e.g. version(Posix) { } else version(Windows) { } else static assert(0, "Unknown OS version."); static assert is a tool like any other and the best way to use it depends on what you're trying to do and what the situation is. - Jonathan M Davis
Mar 29 2011
Jonathan M Davis:If a static assert is in a unit test block it's to verify that something works. You don't necessarily want it in normal code. For instance, what if the static assert is verifying something about a templated type or function? Having that static assert in the normal code would mean that that particular instantiation of the function would always be in the code, whereas putting the static assert in the unit test would only result in that instantiation during unit tests (unless you actually instantiated it in your code).Right. Some static asserts may cause/need the instantiation of a template, that burns both compile time and sometimes some space in the not so well stripped binary. So putting those static asserts just inside the unittests is good. Bye, bearophile
Mar 29 2011