June 09, 2013 Re: about "with statement" | ||||
---|---|---|---|---|
| ||||
Posted in reply to deadalnix | 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.
|
June 09, 2013 Re: about "with statement" | ||||
---|---|---|---|---|
| ||||
Posted in reply to khurshid | 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...
|
June 09, 2013 Re: about "with statement" | ||||
---|---|---|---|---|
| ||||
Posted in reply to deadalnix | 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 |
June 10, 2013 Re: about "with statement" | ||||
---|---|---|---|---|
| ||||
Posted in reply to Ali Çehreli | 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.
|
June 10, 2013 Re: about "with statement" | ||||
---|---|---|---|---|
| ||||
Posted in reply to deadalnix | 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 |
June 10, 2013 Re: about "with statement" | ||||
---|---|---|---|---|
| ||||
Posted in reply to Ali Çehreli | Am Sun, 09 Jun 2013 16:48:39 -0700 schrieb Ali Çehreli <acehreli@yahoo.com>: > > switch(foobar) with(FoobarEnum) { > > // ... > > } > > > > That is golden ! > > +1000 :o) > > 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 -- Marco |
June 10, 2013 Re: about "with statement" | ||||
---|---|---|---|---|
| ||||
Posted in reply to khurshid | 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 |
June 10, 2013 Re: about "with statement" | ||||
---|---|---|---|---|
| ||||
Posted in reply to khurshid | 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(); } |
June 10, 2013 Re: about "with statement" | ||||
---|---|---|---|---|
| ||||
Posted in reply to Marco Leise | 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 |
June 10, 2013 Re: about "with statement" | ||||
---|---|---|---|---|
| ||||
Posted in reply to deadalnix | 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
|
Copyright © 1999-2021 by the D Language Foundation