Please improve the with statement by allowing initialisation

matheus matheus at gmail.com
Thu Mar 13 13:26:06 UTC 2025


On Wednesday, 12 March 2025 at 19:01:46 UTC, Dejan Lekic wrote:
> On Wednesday, 12 March 2025 at 18:55:45 UTC, Dejan Lekic wrote:
>> the with block. Since with statement does not allow 
>> initialisation one would try:
>
> As usual, I forgot to give an example of what I am actually 
> asking for...
>
> ```d
> auto app = new Application();
> with (auto win = new Window) { // currently not allowed
>   setTitle("Example");
>   setDefaultSize(200, 200);
>   setChild(mainBox);
>   app.addWindow(win);
> }
> ```

This should be easy to fix/upgrade, since we can already 
instantiate a class:

import std;

class foo{
     void hi(string who){ writeln("Hi ", who); }
}

void main(){
     auto f = new foo();
     f.hi("There!");
     foo f2;
     with(f2 = new foo()){
         hi("Again!");
     }
}

Hi There!
Hi Again!

One thing missing is Declaration. (I wonder why this is not 
already implemented!?).

Matheus.


More information about the Digitalmars-d mailing list