www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - about "with statement"

reply "khurshid" <khurshod.normuradov gmail.com> writes:
D language have like Pascal/Delphi  "with statement",  which very 
useful for writing readable code.

http://dlang.org/statement.html#WithStatement

Maybe I'm wrong, but, I never saw  where using this statement in 
phobos  source codes, what problem using this statement?

Regards,
Khurshid.
Jun 09 2013
next sibling parent reply Jonathan M Davis <jmdavisProg gmx.com> writes:
On Sunday, June 09, 2013 12:11:23 khurshid wrote:
 D language have like Pascal/Delphi  "with statement",  which very
 useful for writing readable code.
 
 http://dlang.org/statement.html#WithStatement
 
 Maybe I'm wrong, but, I never saw  where using this statement in
 phobos  source codes, what problem using this statement?
I'm not aware of any problems with it, but there's also rarely any reason to use it. In most cases, it doesn't really add anything to the code, and I'd argue that it would make code harder to read, because it hides where the variables are coming from. I don't think that we'd really lose anything if we didn't have it, but it's there if you want to use it, and it's not going away. - Jonathan M Davis
Jun 09 2013
next sibling parent "mike james" <foo bar.com> writes:
On Sunday, 9 June 2013 at 10:22:22 UTC, Jonathan M Davis wrote:
 On Sunday, June 09, 2013 12:11:23 khurshid wrote:
 D language have like Pascal/Delphi  "with statement",  which 
 very
 useful for writing readable code.
 
 http://dlang.org/statement.html#WithStatement
 
 Maybe I'm wrong, but, I never saw  where using this statement 
 in
 phobos  source codes, what problem using this statement?
I'm not aware of any problems with it, but there's also rarely any reason to use it. In most cases, it doesn't really add anything to the code, and I'd argue that it would make code harder to read, because it hides where the variables are coming from. I don't think that we'd really lose anything if we didn't have it, but it's there if you want to use it, and it's not going away. - Jonathan M Davis
Jun 09 2013
prev sibling next sibling parent reply "mike james" <foo bar.com> writes:
On Sunday, 9 June 2013 at 10:22:22 UTC, Jonathan M Davis wrote:
 On Sunday, June 09, 2013 12:11:23 khurshid wrote:
 D language have like Pascal/Delphi  "with statement",  which 
 very
 useful for writing readable code.
 
 http://dlang.org/statement.html#WithStatement
 
 Maybe I'm wrong, but, I never saw  where using this statement 
 in
 phobos  source codes, what problem using this statement?
I'm not aware of any problems with it, but there's also rarely any reason to use it. In most cases, it doesn't really add anything to the code, and I'd argue that it would make code harder to read, because it hides where the variables are coming from. I don't think that we'd really lose anything if we didn't have it, but it's there if you want to use it, and it's not going away. - Jonathan M Davis
Even Wirth gave up on the "with" statement in his later languages design. -<mike>-
Jun 09 2013
parent "bearophile" <bearophileHUGS lycos.com> writes:
mike james:

 Even Wirth gave up on the "with" statement in his later 
 languages design.
I think he was trying to remove all the not essential features from the design of its language, to produce something essential. I prefer a language that has more handy things, even if they are not strictly essential. Today we are still using ObjectPascal (FreePascal) while Oberon and the like are uncommon. Bye, bearophile
Jun 09 2013
prev sibling parent reply "deadalnix" <deadalnix gmail.com> writes:
On Sunday, 9 June 2013 at 10:22:22 UTC, Jonathan M Davis wrote:
 On Sunday, June 09, 2013 12:11:23 khurshid wrote:
 D language have like Pascal/Delphi  "with statement",  which 
 very
 useful for writing readable code.
 
 http://dlang.org/statement.html#WithStatement
 
 Maybe I'm wrong, but, I never saw  where using this statement 
 in
 phobos  source codes, what problem using this statement?
I'm not aware of any problems with it, but there's also rarely any reason to use it. In most cases, it doesn't really add anything to the code, and I'd argue that it would make code harder to read, because it hides where the variables are coming from. I don't think that we'd really lose anything if we didn't have it, but it's there if you want to use it, and it's not going away. - Jonathan M Davis
switch(foobar) with(FoobarEnum) { // ... } That is golden !
Jun 09 2013
next sibling parent Nick Sabalausky <SeeWebsiteToContactMe semitwist.com> writes:
On Sun, 09 Jun 2013 20:09:31 +0200
"deadalnix" <deadalnix gmail.com> wrote:
 
 switch(foobar) with(FoobarEnum) {
      // ...
 }
 
 That is golden !
Oooh! That's a good one! I don't think I've ever used "with" in any language before (never saw much point), but I probably will start using that.
Jun 09 2013
prev sibling next sibling parent reply =?UTF-8?B?QWxpIMOHZWhyZWxp?= <acehreli yahoo.com> writes:
On 06/09/2013 11:09 AM, deadalnix wrote:

 On Sunday, 9 June 2013 at 10:22:22 UTC, Jonathan M Davis wrote:
 I'd argue that it would make code harder to read, because it hides where
 the variables are coming from.
+1. A friend of mine is "still experimenting" with 'with' and I hate reading his code. :) with (new C()) { i = 42; // is that a member of C? j = 43; // how about that? foo(); // and that? bar(); // and that? } Thanks for obfuscating! :)
 switch(foobar) with(FoobarEnum) {
      // ...
 }

 That is golden !
+1000 :o) To me, 'with' is useful only in that case. Ali
Jun 09 2013
next sibling parent "Maxim Fomin" <maxim maxim-fomin.ru> writes:
On Sunday, 9 June 2013 at 23:48:39 UTC, Ali Çehreli wrote:
 On 06/09/2013 11:09 AM, deadalnix wrote:

 On Sunday, 9 June 2013 at 10:22:22 UTC, Jonathan M Davis
wrote:
 I'd argue that it would make code harder to read, because it
hides where
 the variables are coming from.
+1. A friend of mine is "still experimenting" with 'with' and I hate reading his code. :) with (new C()) { i = 42; // is that a member of C? j = 43; // how about that? foo(); // and that? bar(); // and that? } Thanks for obfuscating! :) Ali
import std.stdio; int foo() { writeln("foo"); return 0; } void main() { short i; try switch (i) { writeln(i); default: writeln("default 1"); throw new Exception(""); } catch(Exception e) do switch(foo()) { default: writeln("default 2"); } while(false); finally if (i) switch(i) { writeln("do"); default: } else {} } One bug with this funny stuff sharing common scoped statement is import std.stdio; bool foo() { writeln("foo"); return true; } void main() { short i; switch(i) if (foo()) { default: } else foo(); } It seems that if statement even doesn't reach backend.
Jun 09 2013
prev sibling parent reply Marco Leise <Marco.Leise gmx.de> writes:
Am Sun, 09 Jun 2013 16:48:39 -0700
schrieb Ali =C3=87ehreli <acehreli yahoo.com>:

  > switch(foobar) with(FoobarEnum) {
  >      // ...
  > }
  >
  > That is golden !
=20
 +1000 :o)
=20
 To me, 'with' is useful only in that _case_.
Pun intended? I even remember someone being surprised that it had other uses than this one, but I couldn't find the post. switch(foobar : FoobarEnum) {} ? :p --=20 Marco
Jun 10 2013
parent =?UTF-8?B?QWxpIMOHZWhyZWxp?= <acehreli yahoo.com> writes:
On 06/10/2013 02:43 AM, Marco Leise wrote:

 Am Sun, 09 Jun 2013 16:48:39 -0700
 schrieb Ali Çehreli <acehreli yahoo.com>:

   > switch(foobar) with(FoobarEnum) {
   >      // ...
   > }
 To me, 'with' is useful only in that _case_.
Pun intended?
Ha ha! :) No, I missed that one. Ali
Jun 10 2013
prev sibling next sibling parent Jacob Carlborg <doob me.com> writes:
On 2013-06-09 20:09, deadalnix wrote:

 switch(foobar) with(FoobarEnum) {
      // ...
 }

 That is golden !
I agree, that's basically the only case I've used "with" for. -- /Jacob Carlborg
Jun 10 2013
prev sibling parent "Steven Schveighoffer" <schveiguy yahoo.com> writes:
On Sun, 09 Jun 2013 14:09:31 -0400, deadalnix <deadalnix gmail.com> wrote:

 On Sunday, 9 June 2013 at 10:22:22 UTC, Jonathan M Davis wrote:
 On Sunday, June 09, 2013 12:11:23 khurshid wrote:
 D language have like Pascal/Delphi  "with statement",  which very
 useful for writing readable code.
  http://dlang.org/statement.html#WithStatement
  Maybe I'm wrong, but, I never saw  where using this statement in
 phobos  source codes, what problem using this statement?
I'm not aware of any problems with it, but there's also rarely any reason to use it. In most cases, it doesn't really add anything to the code, and I'd argue that it would make code harder to read, because it hides where the variables are coming from. I don't think that we'd really lose anything if we didn't have it, but it's there if you want to use it, and it's not going away. - Jonathan M Davis
switch(foobar) with(FoobarEnum) { // ... } That is golden !
This is gold plated. Assuming typeof(foobar) == FoobarEnum, any place FoobarEnum is expected, you should not have to specify FoobarEnum. The above should not be necessary. -Steve
Jun 10 2013
prev sibling next sibling parent reply "bearophile" <bearophileHUGS lycos.com> writes:
khurshid:

 D language have like Pascal/Delphi  "with statement",  which 
 very useful for writing readable code.
It's a quite useful statement, especially with enumerations or associative array literals, to avoid repeating many times their name: switch (en) with (MyEnum) { case foo: ... case bar: ... } int[MyEnum] myAA; with (MyEnum) { myAA = [foo: ..., bar: ..., ]; } Instead of: switch (en) { case MyEnum.foo: ... case MyEnum.bar: ... } int[MyEnum] myAA = [ MyEnum.foo: ..., MyEnum.bar: ..., ]; And D with is better than Pascal one, because it removes its main pitfall.
 Maybe I'm wrong, but, I never saw  where using this statement 
 in phobos  source codes, what problem using this statement?
I think it's not used much in Phobos because it's a statement not present in C/C++, and D/Phobos devs are more used to C/C++ languages than to Pascal/Ada ones. Bye, bearophile
Jun 09 2013
next sibling parent reply Andrej Mitrovic <andrej.mitrovich gmail.com> writes:
On 6/9/13, bearophile <bearophileHUGS lycos.com> wrote:
 It's a quite useful statement, especially with enumerations or
 associative array literals, to avoid repeating many times their
 name:

 switch (en) with (MyEnum) {
    case foo: ...
    case bar: ...
 }
Yep, I use this idiom a lot. It's also very useful in unittests, to avoid copy-pasting code and polluting the namespace. A contrived example: ----- class C { int x, y; this() { x = 1; y = 2; } } unittest { with (new C) { assert(x == 1); assert(y == 2); } } ----- There are some bugs with it currently, but I think they're fixable.
Jun 09 2013
parent "Ivan Kazmenko" <gassa mail.ru> writes:
On Sunday, 9 June 2013 at 11:56:42 UTC, Andrej Mitrovic wrote:
 There are some bugs with it currently, but I think they're 
 fixable.
Currently, "with" does not look into "alias this" members which somewhat limits its usefulness: http://d.puremagic.com/issues/show_bug.cgi?id=6711 http://d.puremagic.com/issues/show_bug.cgi?id=9807 (duplicates?)
Jun 09 2013
prev sibling parent reply "khurshid" <khurshod.normuradov gmail.com> writes:
On Sunday, 9 June 2013 at 11:11:47 UTC, bearophile wrote:
 khurshid:

 D language have like Pascal/Delphi  "with statement",  which 
 very useful for writing readable code.
It's a quite useful statement, especially with enumerations or associative array literals, to avoid repeating many times their name: switch (en) with (MyEnum) { case foo: ... case bar: ... }
It's very good example!! And, May be, this is'not bad, too :)) struct C { alias byte B; alias short S; }; with(C) { B b; S s; }
Jun 09 2013
parent "Idan Arye" <GenericNPC gmail.com> writes:
On Sunday, 9 June 2013 at 19:03:12 UTC, khurshid wrote:
 On Sunday, 9 June 2013 at 11:11:47 UTC, bearophile wrote:
 khurshid:

 D language have like Pascal/Delphi  "with statement",  which 
 very useful for writing readable code.
It's a quite useful statement, especially with enumerations or associative array literals, to avoid repeating many times their name: switch (en) with (MyEnum) { case foo: ... case bar: ... }
It's very good example!! And, May be, this is'not bad, too :)) struct C { alias byte B; alias short S; }; with(C) { B b; S s; }
Not very useful, as the scope of `b` and `s` is limited to `with`'s block...
Jun 09 2013
prev sibling next sibling parent Denis Shelomovskij <verylonglogin.reg gmail.com> writes:
09.06.2013 14:11, khurshid пишет:
 D language have like Pascal/Delphi  "with statement",  which very useful
 for writing readable code.

 http://dlang.org/statement.html#WithStatement

 Maybe I'm wrong, but, I never saw  where using this statement in phobos
 source codes, what problem using this statement?
Note, that for now you can't instantiate templates in with statement, see Issue 6196 [1]. [1] http://d.puremagic.com/issues/show_bug.cgi?id=6196 -- Денис В. Шеломовский Denis V. Shelomovskij
Jun 10 2013
prev sibling next sibling parent reply "Anthony Goins" <neontotem gmail.com> writes:
On Sunday, 9 June 2013 at 10:11:25 UTC, khurshid wrote:
 D language have like Pascal/Delphi  "with statement",  which 
 very useful for writing readable code.

 http://dlang.org/statement.html#WithStatement

 Maybe I'm wrong, but, I never saw  where using this statement 
 in phobos  source codes, what problem using this statement?

 Regards,
 Khurshid.
Am I the only one that found this useful? Is there a better way? with (specificModule) { result = ufcsChain.ambiguousFunction.link3(); }
Jun 10 2013
parent Nick Sabalausky <SeeWebsiteToContactMe semitwist.com> writes:
On Mon, 10 Jun 2013 15:33:44 +0200
"Anthony Goins" <neontotem gmail.com> wrote:
 
 Am I the only one that found this useful?
 Is there a better way?
 
 with (specificModule)
 {
      result = ufcsChain.ambiguousFunction.link3();
 }
Ooh, that's another good one!
Jun 10 2013
prev sibling parent "Baz" <Burg.Basile yahoo.com> writes:
On Sunday, 9 June 2013 at 10:11:25 UTC, khurshid wrote:
 D language have like Pascal/Delphi  "with statement",  which 
 very useful for writing readable code.

 http://dlang.org/statement.html#WithStatement

 Maybe I'm wrong, but, I never saw  where using this statement 
 in phobos  source codes, what problem using this statement?

 Regards,
 Khurshid.
You're right but the with statment in Pascal/Delphi is deprecated. While it was usefull in a simple branch, it was error-prone. In "D" the scope() statement can be used to overcome the old Pascal pattern: with whatIcreate try finally free. quote from Delphi XE4 release (technical pdf): "4. OTHER LANGUAGE CHANGES Besides string type changes and objects memory management, there are other current or expected changes in the new Delphi ARM compiler that you can easily start to adopt:  Sooner or later, the with statement is going to be deprecated and removed from the Delphi language. You can easily start removing it now from your code, and most Delphi developer will agree this is a good idea anyway, given some of the hidden pitfalls of this keyword. "
Jun 10 2013