Jump to page: 1 24  
Page
Thread overview
braceless with statements
Nov 12, 2021
Stefan Koch
Nov 12, 2021
Stefan Koch
Nov 12, 2021
kdevel
Nov 12, 2021
user1234
Nov 12, 2021
kdevel
Nov 13, 2021
user1234
Nov 12, 2021
zjh
Nov 12, 2021
Dennis
Nov 12, 2021
Stefan Koch
Nov 12, 2021
Imperatorn
Nov 12, 2021
Dr Machine Code
Nov 12, 2021
Timon Gehr
Nov 15, 2021
Stefan Koch
Nov 12, 2021
user1234
Nov 12, 2021
russhy
Nov 12, 2021
Ogi
Nov 13, 2021
Ogi
Nov 13, 2021
Stefan Koch
Nov 14, 2021
Elronnd
Nov 14, 2021
Ogi
Nov 14, 2021
kdevel
Nov 14, 2021
Ogi
Nov 12, 2021
Stefan Koch
Nov 12, 2021
Stanislav Blinov
Nov 12, 2021
Dr Machine Code
Nov 12, 2021
Ali Çehreli
Nov 12, 2021
Dr Machine Code
Nov 12, 2021
russhy
Nov 12, 2021
Dr Machine Code
November 12, 2021

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.

November 12, 2021

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

>

And now this works:

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

Note: this will actually create an invisible scope from with(x): towards the end of the parent scope.
but it shouldn't affect anything you might do.

I just wanted to point out that using x in the example above will not work it it's done before the with statement.
only after and similarly the names introduced by with will go out of scope when the parent scope leaves.

{
with(s):
 // only vaild for this scope
}
// s.x is not valid here anymore
November 12, 2021

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

>

Good

Good.

November 12, 2021

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.

November 12, 2021

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.

I am not proposing anything grand like that.
I don't think it has a very good fit for if or for
Since their scope tends to end before their parent block scope.

November 12, 2021

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.

November 12, 2021
On Friday, 12 November 2021 at 10:59:39 UTC, Stefan Koch wrote:
[...]
> ```D
> {
> with(s):
>  // only vaild for this scope
> }
> // s.x is not valid here anymore
> ```

That's good, but why stop half way? Drop the colon, drop the parentheses, allow a comma expression:

   A a; B b; ...; Z z;
   :
   with a, b, ..., z;

which is "lowered" to

   with (a) {
      with (b) {
          :
          with (z) {
             :
          }
          :
      }
      :
   }


November 12, 2021
On 12.11.21 11:55, Stefan Koch wrote:
> 
> It is a really simple patch and I think it's worthwhile to have this in the main language.

Yes, please.
November 12, 2021

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

>

Good Morning Everyone,

[...]

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

that's https://issues.dlang.org/show_bug.cgi?id=14332 BTW.

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

« First   ‹ Prev
1 2 3 4