On 07/05/2012 18:06, deadalnix wrote:
Hi,

Working on D I noticed that some statement, notably assert, are expression of 
type void.
Why not all statement (that are not expression already) are expression ?

Because it wouldn't make sense.

The semantic of an ExpressionStatement is to evaluate the expression.

Creating a variable in the scope in which an expression appears, and controlling the flow of the function in which an expression appears, are beyond the scope of the expression evaluation mechanism.

To take an example, what sense would it make if
    return 42
were an expression?  What would its type be?  You would be able to do something 
like
    auto qwert() {
        return return 105;
    }
to instantly return from the function that called qwert.

OK, so assert, or exception throwing more generally, feels like that kind of thing. But exceptions aren't part of the normal flow of program logic. Exceptions can be, and indeed usually are, thrown in the process of evaluating an expression. AssertExpression is an example of this.

Though this does suggest an answer of "because it can be" for why an assert is an expression. Indeed, you could well ask why an assert is an expression but a throw isn't. Maybe it was thought worth convoluting assert to make for concise code by including it in a CommaExpression - but not worth convoluting throw in the same way, since a throw throws unconditionally.

Not that it would be much more complicated to make throw an expression. But you could always define a function of your own like

    void throwEx(Throwable t) { throw t; }

Stewart.

Reply via email to