ES4 draft: assert expression

David Flanagan david at davidflanagan.com
Wed Apr 16 10:08:11 PDT 2008


David Flanagan wrote:

> If there is no way to turn assertions off, and since the spec requires 
> the parentheses around the assertion, I don't see what is gained by 
> making this a expression instead of a global function. If all this spec 
> offers is standardization of the error reporting information in the 
> exception objects, I'm not sure that's worth it.

Replying to myself here...  Is the reason for an assert expression 
instead of an assert function that not having a method invocation makes 
a difference for performance?

Instead of adding 'assert' to the language, have you considered adding 
an 'assertion' keyword instead?

    assertion x > 0, 'x must be positive'

This would be syntax sugar to create an invokeable object with an 
associated message or exception.  The expression doesn't get evaluated 
unless the assertion is actually invoked or asserted or tested...  A 
global assert() method could then accept assertion objects and either 
invoke them or ignore them.  I'm not sure how the performance of this 
would compare to just having regular assertion always on, though. 
Treating assertions as objects does allow programmers to do things like 
start attaching arrays of preconditions and postconditions to methods.

I suspect this is all too complicated, though.  My feeling is that if 
assertions can't be done right (and that includes turning them off) they 
shouldn't be done at all.

	David



More information about the Es4-discuss mailing list