November 12, 2021

On Friday, 12 November 2021 at 12:52:28 UTC, kdevel wrote:

>

That's good, but why stop half way? Drop the colon, drop the parentheses,

parens can be removed that simply:

import std.stdio;
struct L
{
  struct R
  {
      static opUnary(string s : "-")() {writeln("oops");}
  }
}

void main()
{
    with (L) - R;
}

now remove the parens... oops the parser sees a binary minus ;)

November 12, 2021
On Friday, 12 November 2021 at 21:44:25 UTC, user1234 wrote:
> void main()
> {
>     with (L) - R;
> }
> 
> now remove the parens... oops the parser sees a binary minus ;)

that would then translate into

   void main()
   {
       with (L - R) { }
   }

for which the compiler complains about undefined identifier `R`. I must admit I have difficulty to get your point. Under the removed parantheses version you could (additionally) write the original code as

   void main()
   {
       with L;
       - R;
   }
November 13, 2021
On Friday, 12 November 2021 at 23:43:51 UTC, kdevel wrote:
> On Friday, 12 November 2021 at 21:44:25 UTC, user1234 wrote:
>> void main()
>> {
>>     with (L) - R;
>> }
>> 
>> now remove the parens... oops the parser sees a binary minus ;)
>
> that would then translate into
>
>    void main()
>    {
>        with (L - R) { }
>    }
>
> for which the compiler complains about undefined identifier `R`. I must admit I have difficulty to get your point. Under the removed parantheses version you could (additionally) write the original code as
>
>    void main()
>    {
>        with L;
>        - R;
>    }

you can craft code where both work
November 13, 2021

On Friday, 12 November 2021 at 15:33:38 UTC, Steven Schveighoffer wrote:

>

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

I see, thanks.

Can’t say I am a fan of this proposal. Are you in the same scope as a few hundred lines above? With braces and indentation, the answer is clear. Without them, you’ll have to look for with labels that don’t stand out visually at all.

November 13, 2021

On Saturday, 13 November 2021 at 10:43:04 UTC, Ogi wrote:

>

On Friday, 12 November 2021 at 15:33:38 UTC, Steven Schveighoffer wrote:

>

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

I see, thanks.

Can’t say I am a fan of this proposal. Are you in the same scope as a few hundred lines above? With braces and indentation, the answer is clear. Without them, you’ll have to look for with labels that don’t stand out visually at all.

No you just look for the last open brace?
The with statement doesn't behave like a stand alone scope.

November 13, 2021

On 11/13/21 5:43 AM, Ogi wrote:

>

On Friday, 12 November 2021 at 15:33:38 UTC, Steven Schveighoffer wrote:

>

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

I see, thanks.

Can’t say I am a fan of this proposal. Are you in the same scope as a few hundred lines above? With braces and indentation, the answer is clear. Without them, you’ll have to look for with labels that don’t stand out visually at all.

I don't really understand the question. What is the confusion you are having?

Consider the scope(exit) rewrite:

{
   someCode;
   scope(exit) close(foo);
   someCode;
}

This is equivalent to:

{
   someCode;
   try { // new scope!
      someCode;
   } finally {
      close(foo);
   }
}

This introduces a new scope the same as a with: would introduce a new scope (and actually is simpler). Have you had confusion about that feature before? It makes perfect sense to me.

-Steve

November 14, 2021
On Saturday, 13 November 2021 at 14:42:55 UTC, Steven Schveighoffer wrote:
> This introduces a new scope the same as a `with:` would introduce a new scope (and actually is simpler). Have you had confusion about that feature before? It makes perfect sense to me.

FWIW I think with: is a good idea.  However there is an important different between it and scope(exit/...): the latter binds no variables.
November 14, 2021

On Saturday, 13 November 2021 at 14:42:55 UTC, Steven Schveighoffer wrote:

>

I don't really understand the question. What is the confusion you are having?

Braces and indentations keep things sane:

struct S { int x; }
S s;
with (s) {
	/*
	lots of code here
	*/
	x = 42; //is this s.x? The answer is always yes.
}

Now with with::

struct S { int x; }
S s;
with (s):
/*
lots of code here
*/
x = 42; //is this s.x? Depends on the code above.

“Lots of code” can contain anything, including something like this:

struct X {
    void x(int i) {}
}
with (X()):

In this case x isn’t even a variable, it’s a function! But it would still compile.

November 14, 2021

On Sunday, 14 November 2021 at 15:45:13 UTC, Ogi wrote:

[...]

>

Braces and indentations keep things sane:

struct S { int x; }
S s;
with (s) {
	/*
	lots of code here
	*/
	x = 42; //is this s.x? The answer is always yes.
}

Wrong: replace /* lots of code here */ with int x:

$ cat xxx.d
struct S { int x; }
void main ()
{
   S s;
   with (s) {
      int x;
      x = 42; //is this s.x? The answer is always yes.
   }
   import std.stdio;
   writeln (s);
}

$ dmd xxx
$ ./xxx
S(0)
November 14, 2021

On Sunday, 14 November 2021 at 16:51:01 UTC, kdevel wrote:

>

Wrong: replace /* lots of code here */ with int x:

Hmm, it works indeed. Not sure if that’s intended, since the other way around is prohibited:

struct S { int x; }
void main ()
{
   S s;
   int x;
   with (s) {
      x = 42; //Error: with symbol `onlineapp.S.x` is shadowing local symbol `onlineapp.main.x`
   }
}