digitalmars.D - Assert
- David B. Held (7/7) Jun 28 2007 I think assert() will get fixed when we get AST macros. It won't get
- Reiner Pope (6/14) Jun 28 2007 Sounds exciting. (although the prospect of accepting that assert is
- Don Clugston (9/13) Jun 28 2007 Why would you bother? Using DMD 1.015, the code to generate an AST is *v...
- David B. Held (4/7) Jun 28 2007 Not really. Just that Walter is as excited about them as you are, and
- BCS (2/13) Jun 28 2007 what is there to fix? (that is not a rhetorical question)
- David B. Held (21/33) Jun 28 2007 Well, for one, the fact that "assert(obj)" doesn't do what 99% of noobs
- Derek Parnell (9/16) Jun 28 2007 Isn't ...
- Sean Kelly (4/42) Jun 29 2007 Seems like the main problem is that assert is compiled out in release
- David B. Held (5/8) Jun 29 2007 No, you guys are missing the important point: *stringizing the
- Lutger (8/18) Jun 30 2007 Not impossible, but ugly:
- David B. Held (6/16) Jun 30 2007 Well, "stringizing by hand" isn't exactly in the repertoire of
- Don Clugston (3/21) Jun 30 2007 Yes, but it's really only syntactic sugar that's lacking. The functional...
- Bent Rasmussen (5/8) Jul 06 2007 True. This string mixin semantics is powerful, if not quite as discrete ...
- Georg Wrede (14/51) Jun 30 2007 "the builtin will only get called by Phobos" sounds a bit ominous.
- David B. Held (6/17) Jun 30 2007 I agree that denotationally, "ensure" is probably more correct, but
- Sean Kelly (3/20) Jul 01 2007 I've used "predicate" for this before.
- Walter Bright (9/12) Jul 01 2007 Done:
- David B. Held (5/20) Jul 01 2007 Yeah, that's what everyone else said. But what it is missing is
I think assert() will get fixed when we get AST macros. It won't get fixed by Walter, of course, but rather, by us. Everyone will invent their favorite assert(), and the builtin will only get called by Phobos. And then, 10 years down the road, Walter will be writing "The Design and Evolution of D", where he will have to explain why assert(o) doesn't do the obvious thing, even though everyone else's does. ;> Dave
Jun 28 2007
David B. Held wrote:I think assert() will get fixed when we get AST macros. It won't get fixed by Walter, of course, but rather, by us. Everyone will invent their favorite assert(), and the builtin will only get called by Phobos. And then, 10 years down the road, Walter will be writing "The Design and Evolution of D", where he will have to explain why assert(o) doesn't do the obvious thing, even though everyone else's does. ;> DaveSounds exciting. (although the prospect of accepting that assert is wrong just because it can be fixed is thoughtless.) I know we're deep in const at the moment, but is there any news/thoughts on AST macros, to whet our appetites? -- Reiner
Jun 28 2007
Reiner Pope wrote:David B. Held wrote:I think assert() will get fixed when we get AST macros.I know we're deep in const at the moment, but is there any news/thoughts on AST macros, to whet our appetites?Why would you bother? Using DMD 1.015, the code to generate an AST is *very* short (less than 2 lines of code per operator). <g> It's far more flexible than a built-in solution would be. All we need is a bit of syntactic sugar to allow us to generate mixin(func(`x+y`, `"abc"`)); when we type func(x+y, "abc"); We can do everything else already. <g>
Jun 28 2007
Reiner Pope wrote:[...] I know we're deep in const at the moment, but is there any news/thoughts on AST macros, to whet our appetites?Not really. Just that Walter is as excited about them as you are, and wants to get to it Real Soon Now(TM). Dave
Jun 28 2007
Reply to David,I think assert() will get fixed when we get AST macros. It won't get fixed by Walter, of course, but rather, by us. Everyone will invent their favorite assert(), and the builtin will only get called by Phobos. And then, 10 years down the road, Walter will be writing "The Design and Evolution of D", where he will have to explain why assert(o) doesn't do the obvious thing, even though everyone else's does. ;> Davewhat is there to fix? (that is not a rhetorical question)
Jun 28 2007
BCS wrote:Reply to David,Well, for one, the fact that "assert(obj)" doesn't do what 99% of noobs (including me!) expect it to do. Two, you can't write a custom assert() that has non-standard messaging. For instance, there is an extremely common idiom in all languages (that support exceptions) that basically looks like this: if (!everything_ok) { throw SomeException("Helpful message"); } Andrei happens to call this micro-pattern "enforce()", which I think is a perfectly reasonable name for it. What we would like to do is something like so: #define enforce(cond, message) \ if (!cond) throw SomeException(#cond + ": " + message); Bar* ptr = foo(); enforce(ptr != null, "foo() failed"); Obviously there's a thousand ways to riff on that basic idea, but try writing this function in D. As you can see, it's essentially a form of assert(). DaveI think assert() will get fixed when we get AST macros. It won't get fixed by Walter, of course, but rather, by us. Everyone will invent their favorite assert(), and the builtin will only get called by Phobos. And then, 10 years down the road, Walter will be writing "The Design and Evolution of D", where he will have to explain why assert(o) doesn't do the obvious thing, even though everyone else's does. ;>what is there to fix? (that is not a rhetorical question)
Jun 28 2007
On Thu, 28 Jun 2007 23:08:45 -0700, David B. Held wrote:something like so: #define enforce(cond, message) \ if (!cond) throw SomeException(#cond + ": " + message); Bar* ptr = foo(); enforce(ptr != null, "foo() failed");Isn't ... assert(ptr !is null, "foo() failed to supply a valid Bar pointer"); okay to use? -- Derek (skype: derek.j.parnell) Melbourne, Australia 29/06/2007 4:52:03 PM
Jun 28 2007
David B. Held wrote:BCS wrote:Seems like the main problem is that assert is compiled out in release builds, correct? Throwing a custom message and such already works. SeanReply to David,Well, for one, the fact that "assert(obj)" doesn't do what 99% of noobs (including me!) expect it to do. Two, you can't write a custom assert() that has non-standard messaging. For instance, there is an extremely common idiom in all languages (that support exceptions) that basically looks like this: if (!everything_ok) { throw SomeException("Helpful message"); } Andrei happens to call this micro-pattern "enforce()", which I think is a perfectly reasonable name for it. What we would like to do is something like so: #define enforce(cond, message) \ if (!cond) throw SomeException(#cond + ": " + message); Bar* ptr = foo(); enforce(ptr != null, "foo() failed"); Obviously there's a thousand ways to riff on that basic idea, but try writing this function in D. As you can see, it's essentially a form of assert().I think assert() will get fixed when we get AST macros. It won't get fixed by Walter, of course, but rather, by us. Everyone will invent their favorite assert(), and the builtin will only get called by Phobos. And then, 10 years down the road, Walter will be writing "The Design and Evolution of D", where he will have to explain why assert(o) doesn't do the obvious thing, even though everyone else's does. ;>what is there to fix? (that is not a rhetorical question)
Jun 29 2007
Sean Kelly wrote:[...] Seems like the main problem is that assert is compiled out in release builds, correct? Throwing a custom message and such already works.No, you guys are missing the important point: *stringizing the condition*. That is what is impossible without repeating the condition or having macros. Dave
Jun 29 2007
David B. Held wrote:Sean Kelly wrote:Not impossible, but ugly: char[] enforce(char[] cond, char[] msg) { return "if (!(" ~ cond ~ ")) throw new Exception(\"" ~ cond ~ ": " ~ msg ~ "\");"; } mixin(enforce("ptr != null", "foo() failed"));[...] Seems like the main problem is that assert is compiled out in release builds, correct? Throwing a custom message and such already works.No, you guys are missing the important point: *stringizing the condition*. That is what is impossible without repeating the condition or having macros. Dave
Jun 30 2007
Lutger wrote:[...] Not impossible, but ugly: char[] enforce(char[] cond, char[] msg) { return "if (!(" ~ cond ~ ")) throw new Exception(\"" ~ cond ~ ": " ~ msg ~ "\");"; } mixin(enforce("ptr != null", "foo() failed"));Well, "stringizing by hand" isn't exactly in the repertoire of possibility that I was considering, given that we can write any code we want in a mixin statement. Clearly, this kind of solution is less than ideal. Dave
Jun 30 2007
David B. Held wrote:Lutger wrote:Yes, but it's really only syntactic sugar that's lacking. The functionality is almost identical to what you'd want from a macro. We're _so_ close.[...] Not impossible, but ugly: char[] enforce(char[] cond, char[] msg) { return "if (!(" ~ cond ~ ")) throw new Exception(\"" ~ cond ~ ": " ~ msg ~ "\");"; } mixin(enforce("ptr != null", "foo() failed"));Well, "stringizing by hand" isn't exactly in the repertoire of possibility that I was considering, given that we can write any code we want in a mixin statement. Clearly, this kind of solution is less than ideal.Dave
Jun 30 2007
True. This string mixin semantics is powerful, if not quite as discrete as could be wished. It removes redundancy here, but leaves a small syntactic footprint. "Don Clugston" <dac nospam.com.au> wrote in message news:f66c1k$40g$1 digitalmars.com...Yes, but it's really only syntactic sugar that's lacking. The functionality is almost identical to what you'd want from a macro. We're _so_ close.
Jul 06 2007
David B. Held wrote:BCS wrote:Yes, it looks that way.Reply to David,I think assert() will get fixed when we get AST macros."the builtin will only get called by Phobos" sounds a bit ominous.It won't get fixed by Walter, of course, but rather, by us. Everyone will invent their favorite assert(), and the builtin will only get called by Phobos.Hmm. Unfortunately I agree.And then, 10 years down the road, Walter will be writing "The Design and Evolution of D", where he will have to explain why assert(o) doesn't do the obvious thing, even though everyone else's does. ;>DAVID, please, take note: enforce might not be the optimum word for it. I'd rather suggest "ensure". (Yes, David and not Andrei, because I assume English is David's mother tongue, so he's got some influence on Andrei about this.) IMHO, "enforce" is like holding somebody at gunpoint, while "ensure" is like an "ultimate check". In other words, "enforce" ends up (in the real world) in coercing, while "ensure" ends up in "double check this, and if it doesn't work, ask your boss".what is there to fix? (that is not a rhetorical question)Well, for one, the fact that "assert(obj)" doesn't do what 99% of noobs (including me!) expect it to do. Two, you can't write a custom assert() that has non-standard messaging. For instance, there is an extremely common idiom in all languages (that support exceptions) that basically looks like this: if (!everything_ok) { throw SomeException("Helpful message"); } Andrei happens to call this micro-pattern "enforce()", which I think is a perfectly reasonable name for it.What we would like to do is something like so: #define enforce(cond, message) \ if (!cond) throw SomeException(#cond + ": " + message); Bar* ptr = foo(); enforce(ptr != null, "foo() failed"); Obviously there's a thousand ways to riff on that basic idea, but try writing this function in D. As you can see, it's essentially a form of assert().Yes. And if nothing else, this should convince Walter to not have assert() to be inconsistent.
Jun 30 2007
Georg Wrede wrote:[...] DAVID, please, take note: enforce might not be the optimum word for it. I'd rather suggest "ensure". (Yes, David and not Andrei, because I assume English is David's mother tongue, so he's got some influence on Andrei about this.) IMHO, "enforce" is like holding somebody at gunpoint, while "ensure" is like an "ultimate check". In other words, "enforce" ends up (in the real world) in coercing, while "ensure" ends up in "double check this, and if it doesn't work, ask your boss". [...]I agree that denotationally, "ensure" is probably more correct, but connotationally, I think "enforce" is better. "Ensure" has a pansy tone to it that I wouldn't take very seriously; but when I see "enforce", I think "Oh, I'd better take a look at what this is doing." Dave
Jun 30 2007
David B. Held wrote:Georg Wrede wrote:I've used "predicate" for this before. Sean[...] DAVID, please, take note: enforce might not be the optimum word for it. I'd rather suggest "ensure". (Yes, David and not Andrei, because I assume English is David's mother tongue, so he's got some influence on Andrei about this.) IMHO, "enforce" is like holding somebody at gunpoint, while "ensure" is like an "ultimate check". In other words, "enforce" ends up (in the real world) in coercing, while "ensure" ends up in "double check this, and if it doesn't work, ask your boss". [...]I agree that denotationally, "ensure" is probably more correct, but connotationally, I think "enforce" is better. "Ensure" has a pansy tone to it that I wouldn't take very seriously; but when I see "enforce", I think "Oh, I'd better take a look at what this is doing."
Jul 01 2007
David B. Held wrote:Obviously there's a thousand ways to riff on that basic idea, but try writing this function in D. As you can see, it's essentially a form of assert().Done: T Enforce(T)(T p, lazy char[] msg) { if (!p) throw new Exception(msg()); return p; } http://www.digitalmars.com/d/1.0/lazy-evaluation.html
Jul 01 2007
Walter Bright wrote:David B. Held wrote:Yeah, that's what everyone else said. But what it is missing is *stringizing of the condition*, so that you can put it into the message. For Enforce(), that's not as important; but for a custom assert(), it is. DaveObviously there's a thousand ways to riff on that basic idea, but try writing this function in D. As you can see, it's essentially a form of assert().Done: T Enforce(T)(T p, lazy char[] msg) { if (!p) throw new Exception(msg()); return p; } http://www.digitalmars.com/d/1.0/lazy-evaluation.html
Jul 01 2007