Thread overview
First draft: added with-colon statement
Jan 16
Daniel N
Jan 18
qxi
Jan 19
qxi
Jan 19
matheus
January 15
This was requested in the newsgroups. Adds the syntax:

```
{
    with (E):
       statement;
       statement;
}
```

which is equivalent to:

```
{
    with (E)
    {
       statement;
       statement;
    }
}
```

and that's all there is to it.

https://github.com/dlang/dmd/pull/20715
January 16
On Thursday, 16 January 2025 at 06:53:23 UTC, Walter Bright wrote:
> This was requested in the newsgroups. Adds the syntax:
>
> ```
> {
>     with (E):
>        statement;
>        statement;
> }
> ```

cool, thanks!

January 18

On Thursday, 16 January 2025 at 06:53:23 UTC, Walter Bright wrote:

>

This was requested in the newsgroups. Adds the syntax:

{
    with (E):
       statement;
       statement;
}

which is equivalent to:

{
    with (E)
    {
       statement;
       statement;
    }
}

and that's all there is to it.

https://github.com/dlang/dmd/pull/20715

Cool. Doesn’t require a DIP because this is purely additional.

January 18
On Thursday, 16 January 2025 at 06:53:23 UTC, Walter Bright wrote:
> This was requested in the newsgroups. Adds the syntax:
>
> ```
> {
>     with (E):
>        statement;
>        statement;
> }
> ```
>
> which is equivalent to:
>
> ```
> {
>     with (E)
>     {
>        statement;
>        statement;
>     }
> }
> ```
>
> and that's all there is to it.
>
> https://github.com/dlang/dmd/pull/20715

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.

'''
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'
}
'''

January 18
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.
> 
> '''
> 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:
```
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.

January 19
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.
>> 
>> '''
>> 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:
> ```
> 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).
January 19
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.
>> 
>> '''
>> 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:
> ```
> 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.


There is a caveat there for my understanding, because with current/old statement:

import std;

struct S1{ int A;}
struct S2{ int A;}

void main(){
    S1 s1 = {A : 1};
    S2 s2 = {A : 2};

    with(s1){
        writeln(A); // 1
        with (s2)
       		writeln(A); // 2
        writeln(A); // 1
    }
}

Now this could be confusing with ":" and the order of precedence, I think qxi has a point.

Matheus.
January 19

On Thursday, 16 January 2025 at 06:53:23 UTC, Walter Bright wrote:

>

This was requested in the newsgroups. Adds the syntax:

{
    with (E):
       statement;
       statement;
}

which is equivalent to:

{
    with (E)
    {
       statement;
       statement;
    }
}

and that's all there is to it.

https://github.com/dlang/dmd/pull/20715

This seems a bit inconsistent to me with current syntax; with is a statement, and the colon syntax previously only worked at declaration level. i.e.

void foo() {
    @safe:  // can't do this!
}

Otherwise I don't have any comments regarding this syntax, but if we're bringing up changes to with, I would be interested in with as an expression.

auto theme = with (Rule) Theme(
    rule!Node(
        backgroundColor = color("#123"),
        textColor = color("#123"),
    ),
);
// as opposed to
Theme theme;
with (Rule) theme = ...;
February 05

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.

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:

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:

@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:

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).