On Wed, Jul 30, 2014 at 08:12:20AM -0700, Andrei Alexandrescu via Digitalmars-d wrote: > On 7/30/14, 7:55 AM, Tofu Ninja wrote: > >On Wednesday, 30 July 2014 at 14:51:34 UTC, Andrei Alexandrescu wrote: > >>Also, it's unclear to me what the optimizer would be supposed to do > >>if an assumption turns out to be false. > > > >Bad... bad... things... > > So then I see nothing that assume can do that assert can't. -- Andrei
Yeah, I think this hair-splitting between assume and assert is ... pure hair-splitting. I don't know where people got the idea that assert means "check this condition" as opposed to "assume this condition is true and generate code accordingly". My understanding of it is that it's telling the compiler (and whoever reads the code), that at that given juncture in the code, the programmer has assured that a certain condition holds, and that the following code was written with that assumption in mind. The fact that assert will fail at runtime is a secondary artifact to help catch logic errors where the programmer's assumptions somehow got broken. IOW, it's something extra the compiler throws in, in order to help with debugging; it is strictly speaking not part of the program logic. The compiler is merely saying, "OK the programmer says condition X must hold at this point. But just in case he made a mistake, I'll throw in a check that will fail at runtime so that if he *did* make a mistake, the program won't blunder onwards blindly and do stupid things because it made a wrong assumption." It's not *that* far a stretch for the compiler to also say, "OK the programmer says condition X must hold at this point. I trust him, so I'm going to optimize the following code accordingly." To me, it totally makes sense for the compiler to apply the former approach when compiling in non-release mode (which is by definition when the programmer is developing and testing the program, and would appreciate the extra help in assumptions being checked), and the latter approach when compiling in release/optimizing mode (which is by definition where the programmer wants to take maximum advantage of any optimizations the compiler can do -- so optimizations stemming from assert's are fair game as the source of possible code simplifications). T -- Written on the window of a clothing store: No shirt, no shoes, no service.