November 12, 2021

On 11/12/21 10:03 AM, Ogi wrote:

>

On Friday, 12 November 2021 at 10:55:21 UTC, Stefan Koch wrote:

>

Good Morning Everyone,

I recently found myself wanting to introduce a bunch of member variables into the scope of the function I was currently working on.
Of course D has a nice way to do that; The with statement.

so

struct S { int x; }
int fn()
{
    S s;
    with(s)
    {
       x = 12;
       return x;
    }
}

this code works but it forces another level of indentation which makes it a little ugly.

So I did a small patch to my local version of dmd.
And now this works:

struct S { int x; }
int fn()
{
    S s;
    with(s):
    x = 12;
    return x;
}

It is a really simple patch and I think it's worthwhile to have this in the main language.

I’m missing the point. Why using a struct to add some variables? At first I thought that you want to shadow already existing variables but this is prohibited in D and thank God for that.

No, you are wanting to use the members of s many times, and don't want to have to repeat s. all the time. This isn't a great example because of the brevity. But imagine a long expression instead of the single-letter variable.

with already works as noted in the first case, the second case would just be a way to write the same thing but without braces (and indentation).

-Steve

November 12, 2021

On Friday, 12 November 2021 at 15:03:33 UTC, Ogi wrote:

>

On Friday, 12 November 2021 at 10:55:21 UTC, Stefan Koch wrote:

>

[...]

I’m missing the point. Why using a struct to add some variables? At first I thought that you want to shadow already existing variables but this is prohibited in D and thank God for that.

The point is to inject member functions or variables from a struct or class.
it saves some typing.

November 12, 2021

On Friday, 12 November 2021 at 15:50:19 UTC, Stefan Koch wrote:

>

On Friday, 12 November 2021 at 15:03:33 UTC, Ogi wrote:

>

On Friday, 12 November 2021 at 10:55:21 UTC, Stefan Koch wrote:

>

[...]

I’m missing the point. Why using a struct to add some variables? At first I thought that you want to shadow already existing variables but this is prohibited in D and thank God for that.

The point is to inject member functions or variables from a struct or class.
it saves some typing.

Or enums. Enums. ENUMS!!! :)

November 12, 2021

On Friday, 12 November 2021 at 11:26:41 UTC, Imperatorn wrote:

>

On Friday, 12 November 2021 at 11:05:54 UTC, Dennis wrote:

>

On Friday, 12 November 2021 at 10:55:21 UTC, Stefan Koch wrote:

>

It is a really simple patch and I think it's worthwhile to have this in the main language.

How about other statements, like if, for, foreach, switch? When extending attribute: syntax to statements, might as well include all of them for consistency. N.b. this is already allowed in global scope:

static if (true):

Not in function scope though.

static if (true) should be banned from existence, use comments or delete if false.

why not use rather version(none) and version(all) ?

November 12, 2021

On Friday, 12 November 2021 at 10:55:21 UTC, Stefan Koch wrote:

>

Good Morning Everyone,

I recently found myself wanting to introduce a bunch of member variables into the scope of the function I was currently working on.
Of course D has a nice way to do that; The with statement.

so

struct S { int x; }
int fn()
{
    S s;
    with(s)
    {
       x = 12;
       return x;
    }
}

this code works but it forces another level of indentation which makes it a little ugly.

So I did a small patch to my local version of dmd.

Do you use your own compiler version? do use it for anything but toy projects?

>

And now this works:

struct S { int x; }
int fn()
{
    S s;
    with(s):
    x = 12;
    return x;
}

It is a really simple patch and I think it's worthwhile to have this in the main language.

it's lowered to the very same code in your first example, right? tbh honest I like it, wouldn't mind that in the main language. I think this one of the syntax sugar examples that didn't make it hard and/or ugly to read

November 12, 2021

On Friday, 12 November 2021 at 17:45:05 UTC, Stanislav Blinov wrote:

>

On Friday, 12 November 2021 at 15:50:19 UTC, Stefan Koch wrote:

>

On Friday, 12 November 2021 at 15:03:33 UTC, Ogi wrote:

>

On Friday, 12 November 2021 at 10:55:21 UTC, Stefan Koch wrote:

>

[...]

I’m missing the point. Why using a struct to add some variables? At first I thought that you want to shadow already existing variables but this is prohibited in D and thank God for that.

The point is to inject member functions or variables from a struct or class.
it saves some typing.

Or enums. Enums. ENUMS!!! :)

what's wrong with them?

November 12, 2021
On 11/12/21 11:47 AM, Dr Machine Code wrote:

>> Or enums. Enums. ENUMS!!! :)
> 
> what's wrong with them?

They are great. :) 'with' helps especially with switch statements:

enum LongEnumName {
  a, b, c
}

void main() {
  LongEnumName e;

  // 'LongEnumName.' is repeated for each case:
  final switch (e) {
  case LongEnumName.a:
  case LongEnumName.b:
  case LongEnumName.c:
  }

  // Same thing but shorter syntax:
  final switch (e) with (LongEnumName) {
  case a:
  case b:
  case c:
  }
}

Ali
November 12, 2021
On Friday, 12 November 2021 at 20:52:22 UTC, Ali Çehreli wrote:
> On 11/12/21 11:47 AM, Dr Machine Code wrote:
>
>>> Or enums. Enums. ENUMS!!! :)
>> 
>> what's wrong with them?
>
> They are great. :) 'with' helps especially with switch statements:
>
> enum LongEnumName {
>   a, b, c
> }
>
> void main() {
>   LongEnumName e;
>
>   // 'LongEnumName.' is repeated for each case:
>   final switch (e) {
>   case LongEnumName.a:
>   case LongEnumName.b:
>   case LongEnumName.c:
>   }
>
>   // Same thing but shorter syntax:
>   final switch (e) with (LongEnumName) {
>   case a:
>   case b:
>   case c:
>   }
> }
>
> Ali

oh I see, I do use that as well. Very helpful ;)
November 12, 2021

I approve!

November 12, 2021

On Friday, 12 November 2021 at 17:45:05 UTC, Stanislav Blinov wrote:

>

On Friday, 12 November 2021 at 15:50:19 UTC, Stefan Koch wrote:

>

On Friday, 12 November 2021 at 15:03:33 UTC, Ogi wrote:

>

On Friday, 12 November 2021 at 10:55:21 UTC, Stefan Koch wrote:

>

[...]

I’m missing the point. Why using a struct to add some variables? At first I thought that you want to shadow already existing variables but this is prohibited in D and thank God for that.

The point is to inject member functions or variables from a struct or class.
it saves some typing.

Or enums. Enums. ENUMS!!! :)

i still believe enums shouldn't need one to use 'with(MyEnumType)'

'with' shouldn't be an argument for "we have it at home" kind of thing