"using the result of a comma expression is not allowed"

Don Allen donaldcallen at gmail.com
Sun Apr 21 19:16:03 UTC 2024


Thank you both for your useful replies and I understand why D has 
parted company with C in this area.

But I maintain that the documentation is in conflict with itself 
and should be fixed (11.1 could note that there are exceptions, 
such as comma expressions). And I think it's bad practice to 
support a hobbled version of a common construct in anticipation 
of a use of the same syntax that hasn't happened yet and has been 
in the pipeline for years.

I am also not about to re-write my code using lambdas. It's 
hackish workaround for a defect in the language, in my opinion.

And this whole discussion brings up the issue, for me, that 'if' 
and blocks are not expressions that can yield values. Both Rust 
and Zig support this. The separate and redundant conditional 
statement and expression in D is a holdover from C. I think this 
was a mistake in C and a bigger mistake in D, since we know more 
now about language design than when Dennis Ritchie designed C 
about 55 years ago.

On Sunday, 21 April 2024 at 18:31:55 UTC, Steven Schveighoffer 
wrote:
> On Sunday, 21 April 2024 at 16:28:47 UTC, Don Allen wrote:
>> I just ran across "using the result of a comma expression is 
>> not allowed" in a situation where I used a comma expression as 
>> I would in C, expecting its value to be the value of the 
>> right-most expression.
>>
>> While D's language document is accurate in explaining how the 
>> comma expression is treated in D in 11.5, that treatment 
>> conflicts with 11.1:
>> "Expressions are used to compute values with a resulting type. 
>> These values can then be assigned, tested, or ignored. 
>> Expressions can also have side effects."
>>
>> This restriction makes no sense to me, nor does calling this 
>> an "expression" if you can't use its value.
>
> Comma expressions can make code with typos be accepted causing 
> confusing problems.
>
> The compromise, since comma expressions are useful in the 
> update section of `for` loops, was to simply disallow using the 
> value of the expression.
>
> In my experience with C, by far the most common use of comma 
> expressions is to expand macros in expression context. 
> Obviously, we don't need that in D, you can do the same thing 
> with a lambda in D.
>
> A very notorious misuse of comma expressions was in Andrei's 
> "The D Programming Language" book, where it was stated that 
> `synchronized(A, B)` would protect the following block by 
> locking `A` and `B` in the proper sequence. However, this 
> feature was not implemented, instead, `A, B` was a comma 
> expression, and only `B` was locked.
>
> As also noted by others, we may reuse the comma expression 
> syntax for tuples somehow, which only works if you can't use 
> comma expressions to mean "only the last thing".
>
> -Steve




More information about the Digitalmars-d mailing list