type switch

Dennis Ritchie via Digitalmars-d digitalmars-d at puremagic.com
Mon May 4 15:48:35 PDT 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?


More information about the Digitalmars-d mailing list