Thread overview | ||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
June 09, 2013 about "with statement" | ||||
---|---|---|---|---|
| ||||
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. |
June 09, 2013 Re: about "with statement" | ||||
---|---|---|---|---|
| ||||
Posted in reply to khurshid | 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
|
June 09, 2013 Re: about "with statement" | ||||
---|---|---|---|---|
| ||||
Posted in reply to khurshid | 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 |
June 09, 2013 Re: about "with statement" | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jonathan M Davis | 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
|
June 09, 2013 Re: about "with statement" | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jonathan M Davis | 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>-
|
June 09, 2013 Re: about "with statement" | ||||
---|---|---|---|---|
| ||||
Posted in reply to mike james | 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
|
June 09, 2013 Re: about "with statement" | ||||
---|---|---|---|---|
| ||||
Posted in reply to bearophile | 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.
|
June 09, 2013 Re: about "with statement" | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jonathan M Davis | 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 !
|
June 09, 2013 Re: about "with statement" | ||||
---|---|---|---|---|
| ||||
Posted in reply to Andrej Mitrovic | 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?) |
June 09, 2013 Re: about "with statement" | ||||
---|---|---|---|---|
| ||||
Posted in reply to bearophile | 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;
}
|
Copyright © 1999-2021 by the D Language Foundation