type switch

Dennis Ritchie via Digitalmars-d digitalmars-d at puremagic.com
Tue May 5 19:49:39 PDT 2015


On Tuesday, 5 May 2015 at 18:12:42 UTC, Ali Çehreli wrote:
> Personally, I haven't felt the need for this becuase usually 
> there is something different only for one or two specific types.
>
> I don't see that the syntax becomes shorter to warrant a 
> language addition either. (However, the risk of writing 'else 
> if' instead of 'else static if' is a real annoyance that comes 
> with bad consequences.)
>
> Another option is to use template specializations, which can be 
> mixed-in (in bar() below) or called directly (in foo() below):
>
> void doSomethingSpecial(T : int)()
> {
>     // ...
> }
>
> void doSomethingSpecial(T : string)()
> {
>     // ...
> }
>
> void foo(T)()
> {
>     doSomethingSpecial!T();
> }
>
> void bar(T)()
> {
>     mixin doSomethingSpecial!T;
> }
>
> void main()
> {
>     foo!string();
>     bar!int();
> }
>
> Ali

Maybe it will not much shorter, but in some cases, such a design 
would look more appropriate than static if.
In addition, the keyword "case" may be omitted in this design, 
which will make it even more short and readable. Such as here:
http://dpaste.dzfl.pl/d6643ec8ccd3

In my opinion, the implementation of such a structure will not 
hurt D. While it may be something I do not understand :)


More information about the Digitalmars-d mailing list