www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Extend with to take multiple arguments

reply Amex <Amex gmail.com> writes:
with(A){with(B){with(C){
is quite annoying...

Rather,

allow

with(A,B,C){

Also, B is checked to come from A so one does not have to do

with(A, A.B, A.B.C)

also

have an opWith which is similar to opDispatch for With overriding.
Jun 04 2019
parent reply ag0aep6g <anonymous example.com> writes:
On 05.06.19 02:57, Amex wrote:
 with(A){with(B){with(C){
 is quite annoying...
You don't need the braces. with (A) with (B) with (C) { ... }
Jun 04 2019
parent reply Andrei Alexandrescu <SeeWebsiteForEmail erdani.com> writes:
On 6/5/19 1:15 AM, ag0aep6g wrote:
 On 05.06.19 02:57, Amex wrote:
 with(A){with(B){with(C){
 is quite annoying...
You don't need the braces. with (A) with (B) with (C) {     ... }
Wow this must be the shortest and most satisfying thread ever.
Jun 05 2019
parent reply 12345swordy <alexanderheistermann gmail.com> writes:
On Wednesday, 5 June 2019 at 20:10:40 UTC, Andrei Alexandrescu 
wrote:
 On 6/5/19 1:15 AM, ag0aep6g wrote:
 On 05.06.19 02:57, Amex wrote:
 with(A){with(B){with(C){
 is quite annoying...
You don't need the braces. with (A) with (B) with (C) {     ... }
Wow this must be the shortest and most satisfying thread ever.
you can initialize a class like this: var exp = new Class { }; That is just my opinion though. Alex
Jun 05 2019
next sibling parent reply Adam D. Ruppe <destructionator gmail.com> writes:
On Wednesday, 5 June 2019 at 21:02:39 UTC, 12345swordy wrote:
 tbh I found the with keyword to be unnecessarily verbiage. In 

 var exp = new Class { };
What does that have to do with the `with` keyword?
Jun 05 2019
parent reply Laurent =?UTF-8?B?VHLDqWd1aWVy?= <laurent.treguier.sink gmail.com> writes:
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 

 var exp = new Class { };
What does that have to do with the `with` keyword?
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.
Jun 06 2019
next sibling parent Adam D. Ruppe <destructionator gmail.com> writes:
On Thursday, 6 June 2019 at 07:19:15 UTC, Laurent Tréguier wrote:

 directly initialize class members in the curly brackets:
Oh yeah, I see, I usually use with for existing things, like with(MyEnum) ... so it didn't come to my mind.
Jun 06 2019
prev sibling parent Amex <Amex gmail.com> writes:
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 

 var exp = new Class { };
What does that have to do with the `with` keyword?
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).
Jun 06 2019
prev sibling parent reply Bastiaan Veelo <Bastiaan Veelo.net> writes:
On Wednesday, 5 June 2019 at 21:02:39 UTC, 12345swordy wrote:

 var exp = new Class { };

 That is just my opinion though.

 Alex
I don’t understand your point. How is that different from D? auto exp = new class {}; https://run.dlang.io/is/mupnDT Bastiaan.
Jun 05 2019
parent reply Simen =?UTF-8?B?S2rDpnLDpXM=?= <simen.kjaras gmail.com> writes:
On Thursday, 6 June 2019 at 06:53:00 UTC, Bastiaan Veelo wrote:
 On Wednesday, 5 June 2019 at 21:02:39 UTC, 12345swordy wrote:

 var exp = new Class { };

 That is just my opinion though.

 Alex
I don’t understand your point. How is that different from D? auto exp = new class {}; https://run.dlang.io/is/mupnDT Bastiaan.
First, new Class has different capitalization, so it's not an anonymous class like in your code. Second, I believe 12345swordy omitted some parts of his intended code, so a more correct example would be: 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
Jun 06 2019
parent reply 12345swordy <alexanderheistermann gmail.com> writes:
On Thursday, 6 June 2019 at 07:22:38 UTC, Simen Kjærås wrote:
 On Thursday, 6 June 2019 at 06:53:00 UTC, Bastiaan Veelo wrote:
 [...]
First, new Class has different capitalization, so it's not an anonymous class like in your code. Second, I believe 12345swordy omitted some parts of his intended code, so a more correct example would be: 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
Jun 06 2019
parent Bastiaan Veelo <Bastiaan Veelo.net> writes:
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.
Jun 06 2019