Why not all statement are expressions ?

deadalnix deadalnix at gmail.com
Wed May 9 06:18:21 PDT 2012


Le 09/05/2012 14:11, Stewart Gordon a écrit :
> 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?

The same as currently.

> What would its type be?

void

> You would be able to do
> something like
> auto qwert() {
> return return 105;
> }
> to instantly return from the function that called qwert.
>

No it is incorrect. but not grammatically, semantically.

Just like 3 + foo() is incorrect if foo's return type is void. It is 
grammatically correct.

> OK, so assert, or exception throwing more generally, feels like that
> kind of thing.

They don't feel like. It is specified (and even implemented, look in dmd 
source code) that assert is an expression for instance. Expression that 
is of type void (and so have no value).

As you are unable to explain why such a thing exist, I really doubt you 
are qualified to answer that question at all. I'm asking here about 
design decisions.

If you have no clue what I'm talking about please do not answer, or ask 
for more details.


More information about the Digitalmars-d mailing list