Variable declaration primary expression

IchorDev zxinsworld at gmail.com
Tue Jan 21 13:26:40 UTC 2025


On Tuesday, 21 January 2025 at 01:18:08 UTC, Richard (Rikki) 
Andrew Cattermole wrote:
> I just mean that if there are no problems in the grammar then 
> it would be a good addition.

Absolutely agreed. I think the codegen would be the bigger issue, 
surely? I’m not sure if all the existing special cases reuse the 
same code but I have a feeling they don’t.
Do you think it should be necessary to explicitly initialise the 
variable (e.g. `auto x = y`) or should default initialisation be 
possible? I’m not sure why anyone would want to use default 
initialisation?
Also if this syntax were to work in declaration lists then it 
should only declare a variable for the duration of a single 
expression, hence:
```d
module example;
bool x = int* y = ctfeFn() && *y == 2;
auto z = y; //Error: no variable `y` in scope
```
However this is probably too complex to implement since it adds a 
whole new variable scope, and you could just use a CTFE lambda:
```d
module example;
bool x = { int* y = ctfeFn(); return *y == 2; }();
auto z = y; //Error: no variable `y` in scope
```
Ultimately the purpose of this syntax would be making 
declarations inside of flow control statements. I don’t think it 
would be a huge shame if it were forbidden elsewhere, but let me 
know if you think of any other cool use-cases.


More information about the dip.ideas mailing list