On 2011-05-30 16:34, Stewart Gordon wrote: > On 29/05/2011 14:03, bearophile wrote: > > Stewart Gordon: > >> There are places where the spec fails to make a clear distinction > >> between illegal code and incorrect code that the compiler may reject if > >> it's smart enough. > > > > In D there are pure functions, so I think it's not too much hard for it > > to tell apart when the contents of an assert() are pure or not. > > My opinion is that the D compiler has to enforce purity inside assert(), > > to avoid bugs. > > Only if purity rules are relaxed. AIUI, one of the restrictions at the > moment is that in a pure function only immutable data can be accessed. As > long as this restriction remains in place, adding the restriction of > purity to asserts would erode their usefulness.
That's not quite true. Pure functions cannot access global or static variables which are not immutable. However, they can access mutable data in objects just fine. However, unless their parameters are all either immutable or implicitly convertible to immutable, then they are weakly pure and cannot be optimized out like strongly pure functions can (the immutability of the function's parameters being the deciding factor for weak vs strong). Really, the problem with enforcing that assertions have pure expressions is the fact that there is a _ton_ of valid code which will never be able to be pure, and it would be _highly_ annoying to not be able to call it in assertions. Many unittest blocks would double in size, because they would have to save all of their results as local variables and test the variables instead of just testing the expressions directly. It would be horrific if you couldn't put non-pure functions in assertions. Granted, the programmer needs to be somewhat intelligent about what they put in assertions (any calls which actually have side effects are likely to cause bugs due to the fact that the code changes when the assertions go away), but using purity as a way to enforce a lack of side effects would have _far_ too many negative effects to be worthwhile. Regardless, Walter has already shot down the idea ( http://d.puremagic.com/issues/show_bug.cgi?id=6074 ), and I _really_ doubt anyone is going to be able to convince him. - Jonathna M Davis