A couple of extensions to `with` that would be worthwhile

Tejas notrealemail at gmail.com
Thu Oct 14 13:50:29 UTC 2021


On Thursday, 14 October 2021 at 13:44:24 UTC, Paul Backus wrote:
> On Thursday, 14 October 2021 at 07:12:29 UTC, Tejas wrote:
>> Maybe having the syntax as:
>> ```d
>> with <construct> as <identifier>{
>>     //do stuff
>> }
>> ```
>> Would be better?
>> It could also be useful in `Python` style management for 
>> `file` handles, plus be visually distinct enough from other 
>> uses of `with` to not simply be overlooked.
>
> In D this is accomplished using a regular block scope and a 
> variable declaration:
>
> ```d
> {
>     auto <identifier> = <construct>;
>     // do stuff
> }
> ```
>
> Python needs special syntax for it because Python doesn't have 
> block scopes--the only thing that introduces a new scope in 
> Python is a function.

So is it safe to say what Andrei wants can be accomplished with 
new block + `with`?
```d
void main(){
     int a;
     struct S{ int d;}
     {
        S s;
        with(s){
          a = d;
        }
     }
}
```

I kinda remembered that Python can't introduce new scope 
arbitrarily when jfondren criticized my opinion as well.

Is this proposed extension merely syntax sugar, then?


More information about the Digitalmars-d mailing list