June 06, 2019
On Thursday, 6 June 2019 at 14:36:57 UTC, 12345swordy wrote:
> On Thursday, 6 June 2019 at 07:22:38 UTC, Simen Kjærås wrote:
>>     class Class {
>>         int n;
>>         string s;
>>     }
>>
>>     var exp = new Class { n = 4, s = "foo" };
>>
>> This, D doesn't do. The equivalent would be:
>>
>>     class Class {
>>         int n;
>>         string s;
>>     }
>>
>>     auto exp = new Class();
>>     with (exp) {
>>         n = 4;
>>         s = "foo";
>>     }
>>
>> --
>>   Simen
>
> Exactly, what I meant, thank you Simen.
>
> Alex

I see. I played with this a bit. Just for fun, there is an alternative without “with” and without repeating the object name. It’s not shorter nor prettier, and it’s not quite the same :-) Anyway:

   auto exp = new class Class { this() {
        n = 4; s = "foo";
   } };

https://run.dlang.io/is/EcC8KK

Bastiaan.
June 06, 2019
On Thursday, 6 June 2019 at 07:19:15 UTC, Laurent Tréguier wrote:
> On Thursday, 6 June 2019 at 00:29:33 UTC, Adam D. Ruppe wrote:
>> On Wednesday, 5 June 2019 at 21:02:39 UTC, 12345swordy wrote:
>>> tbh I found the with keyword to be unnecessarily verbiage. In C# you can initialize a class like this:
>>> var exp = new Class { };
>>
>> What does that have to do with the `with` keyword?
>
> I think that what 12345swordy means is that in C#, you can directly initialize class members in the curly brackets:
>
> var exp = new Something() {
>     SomeMember = 42
> }
>
> So you get shorter syntax to init class members, without the need for a "with" keyword.

I don't use with just to initalize but many things... it cuts down having to specify a reference.

with(XX) with(YY)
{
   swith(q}
   {
      case a
   }

   if (b)

   foo(y)
   }
}


where q,a,b,y all come from XX and YY.

Having to specify all the withs gets redundant(since it has to be done for each scope, I usually use it at the function level).

1 2
Next ›   Last »