Why not all statement are expressions ?

Jakob Ovrum jakobovrum at gmail.com
Tue May 8 11:40:31 PDT 2012


On Tuesday, 8 May 2012 at 18:30:08 UTC, Timon Gehr wrote:
> On 05/08/2012 10:46 AM, Don Clugston wrote:
>> On 07/05/12 19: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 ?
>>
>> assert isn't a statement. It's an expression ( same as is() ). 
>> What
>> makes you think it's a statement?
>>
>> The main use for a void expression is so that it can be used 
>> in a comma
>> expression; this is why assert is an expression.
>>
>> The curious thing, which may be the source of the confusion, 
>> is that
>> static assert() is a statement, while assert() is an 
>> expression. Maybe
>> static assert should also be an expression rather than a 
>> statement?
>
> static assert is a declaration.

It's a statement when used inside functions, and a "declaration" 
(DeclarationDefinition seems to be the spec's term for top-level 
constructs) elsewhere. Other syntactic constructs have a similar 
duality, e.g. import.

Everything inside a function is a statement, including variable 
declarations; they are declaration statements.



More information about the Digitalmars-d mailing list