digitalmars.D - Re: A switch/case too far...
- terranium <spam here.lot> May 22 2008
- "Janice Caron" <caron800 googlemail.com> May 22 2008
- terranium <spam here.lot> May 22 2008
- "Janice Caron" <caron800 googlemail.com> May 22 2008
- "Koroskin Denis" <2korden gmail.com> May 22 2008
Janice Caron Wrote:I like the following idea: if the case statement specifies a type, check that the object can be implicitly cast to that type; if the case statement specifies a value, check for equality. So: switch(typeof(a)) { case B1: // RTTI dynamic cast test // ... case B2: // ... }
May 22 2008
2008/5/22 terranium <spam here.lot>:there is no need for RTTI for *implicit* cast.
I know. So what?
May 22 2008
Janice Caron Wrote:2008/5/22 terranium <spam here.lot>:there is no need for RTTI for *implicit* cast.
I know. So what?
so what's the meaning ofcheck that the object can be implicitly cast to that type switch(typeof(a)) { case B1: // RTTI dynamic cast test }
May 22 2008
Well, convenience and readability, of course.
switch(typeof(a))
{
case B1:
// stuff
break;
case B2:
// stuff
break;
case B3:
// stuff
break;
}
looks cooler than
{
auto t = cast(B1)a;
if (t != null)
{
// stuff
}
}
{
auto t = cast(B2)a;
if (t != null)
{
// stuff
}
}
{
auto t = cast(B3)a;
if (t != null)
{
// stuff
}
}
(in my opinion, at least). That said, I don't really care much about
this proposal one way or the other. It's only sugar, after all.
May 22 2008
On Thu, 22 May 2008 17:30:14 +0400, Janice Caron <caron800 googlemail.com> wrote:Well, convenience and readability, of course. [skip] (in my opinion, at least). That said, I don't really care much about this proposal one way or the other. It's only sugar, after all.
terranium most probably found your fault with the following:check that the object can be implicitly cast to that type
Since implicit cast always succeeds, there is no need for such a switch, that's why it should work with *explicit* cast only, it's an error otherwise: class A { } class B : A { } class C { } B a = new B(); switch (b is) { case A: // always succeeds, and therefore makes no sense break; case C: doSomething(); // error, unreachable code break; }
May 22 2008









terranium <spam here.lot> 