Thread overview
type switch
May 04, 2015
Dennis Ritchie
May 05, 2015
Dennis Ritchie
May 05, 2015
Ali Çehreli
May 06, 2015
Dennis Ritchie
May 05, 2015
Justin Whear
May 06, 2015
Dennis Ritchie
May 04, 2015
Good day to all!

I've been thinking. In D you can write a similar design to the generic functions or somewhere, use static if:

// -----
static if (is(T == short) || is(T == int) || is(T == long)) {
	// do anything
} else static if (is(T == real)) {
	// ...
} else static if (is(T == char)) {
	// ...
} else static if (is(T == string)) {
	// ...
} else static if (is(T == int[])) {
	// ...
} else {
	// ...
}

Why not put in Phobos shorter design type switch:

// -----
type switch (T) {
case short, int, long:
	// do anything
case real:
	// ...
case char:
	// ...
case string:
	// ...
case int[]:
	// ...
default:
	// ...
}

This design has been implemented, for example, in Common Lisp (typecase):
http://www.lispworks.com/documentation/lw51/CLHS/Body/m_tpcase.htm

What do you think about this?
May 05, 2015
On Monday, 4 May 2015 at 22:48:37 UTC, Dennis Ritchie wrote:
> Why not put in Phobos shorter design type switch:

*More precisely, not in Phobos and DMD.
May 05, 2015
On 05/04/2015 03:48 PM, Dennis Ritchie wrote:

> I've been thinking. In D you can write a similar design to the generic
> functions or somewhere, use static if:
>
> // -----
> static if (is(T == short) || is(T == int) || is(T == long)) {
>      // do anything
> } else static if (is(T == real)) {
>      // ...
> } else static if (is(T == char)) {
>      // ...
> } else static if (is(T == string)) {
>      // ...
> } else static if (is(T == int[])) {
>      // ...
> } else {
>      // ...
> }
>
> Why not put in Phobos shorter design type switch:
>
> // -----
> type switch (T) {
> case short, int, long:
>      // do anything
> case real:
>      // ...
> case char:
>      // ...
> case string:
>      // ...
> case int[]:
>      // ...
> default:
>      // ...
> }
>
> This design has been implemented, for example, in Common Lisp (typecase):
> http://www.lispworks.com/documentation/lw51/CLHS/Body/m_tpcase.htm
>
> What do you think about this?

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

May 05, 2015
How's this? http://dpaste.dzfl.pl/d6643ec8ccd3
May 06, 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 :)
May 06, 2015
On Tuesday, 5 May 2015 at 18:37:52 UTC, Justin Whear wrote:
> How's this? http://dpaste.dzfl.pl/d6643ec8ccd3

Yes, something like that. In my opinion, it's really easier than static if in some cases.