First draft: added with-colon statement

Quirin Schroll qs.il.paperinik at gmail.com
Wed Feb 5 13:54:02 UTC 2025


On Sunday, 19 January 2025 at 10:34:34 UTC, qxi wrote:
> On Sunday, 19 January 2025 at 05:25:15 UTC, Walter Bright wrote:
>> On 1/18/2025 1:19 PM, qxi wrote:
>>> In current implementation if there is multiple 'with:' in 
>>> same scope then last 'with:' take precedence and I think 
>>> multiple 'with:' in the same scope should have the equal 
>>> precedence.
>>> 
>>> ```d
>>> enum E1{ A,B }
>>> 
>>> enum E2{ B,C }
>>> 
>>> {
>>>    with (E1):
>>>    B;  // E1.B
>>> 
>>>    with (E2):
>>> 
>>>    A;  // E1.A
>>>    C;  // E2.C
>>>    B;  // this should be error because 2 matched symbols 
>>> 'E1.B' and 'E2.B'
>>> }
>>> ```
>>
>>
>> The example is equivalent to:
>> ```d
>> enum E1{ A,B }
>>
>> enum E2{ B,C }
>>
>> {
>>    with (E1)
>>    {
>>      B;  // E1.B
>>
>>      with (E2)
>>      {
>>        A;  // E1.A
>>        C;  // E2.C
>>        B;  // (*)
>>      }
>>    }
>> }
>> ```
>> and B should resolve as E2.B because of the usual scoping 
>> rules. Changing the scoping rules would be different from 
>> other uses of : that introduce scope.
>
> I think 'with:' should just import symbols to current scope 
> instead introducing new scope.
> Other uses ':' don't introduce scope according to documentation 
> (at least it dont say it explicitly, it may introduce scope 
> internally in compiler but that I dont know).

That’s simply not true. On declaration scope:
```d
@safe:
@system:
void f() {} // is @system because @system after @safe
```

If we wanted it to behave like `import`, do it like `import`, 
i.e. no parentheses and a semicolon:

```d
with E1;
A; // E1.A
B; // E1.B;
C; // error: No `C` defined

with E2;
A; // E1.A
B; // error: ambiguous, could be E1.B or E2.B
C; // E2.C
```

I’d have little issue having both, actually.
* `with(E):` is equivalent to `with(E) { }` such that the scope 
spans the rest of the function.
* `with E;` is really new and allows for the constants within `E` 
to be found as if they were imported. Also only applies to the 
rest of the scope, but multiple `with E;` don’t nest (exactly 
like imports don’t nest).


More information about the dip.development mailing list