"using the result of a comma expression is not allowed"
Paul Backus
snarwin at gmail.com
Sun Apr 21 18:01:45 UTC 2024
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.
IIRC the rationale here was to reserve the comma operator for use
in tuple expressions (like in Python).
If you the behavior of a C-style comma expression, you can use a
function literal:
// `e1, e2` becomes
() { e1; return e2; }()
If `e2` is an lvalue, you may want to prefix the function literal
with `ref`.
More information about the Digitalmars-d
mailing list