www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Signature constraints on templated enums and aliases?

Recently, very nice syntactic sugar was added for eponymous templates
that expand to enums or aliases:

	// Original verbose form
	template myEnum(T) {
		enum myEnum = T.someStaticValue;

	// New concise form:
	enum myEnum(T) = T.someStaticValue;

	// Original verbose form
	template MyType(T) {
		alias MyType = typeof(T.someField);

	// New concise form:
	alias MyType(T) = T.someField;

This is wonderful, since it lets us shorten common templated enums and
aliases the same way templated functions are. However, there's one
missing component: signature contraints.

	// sig constraints work for templated functions:
	auto myFunc(T)(T t) if (isInputRange!T) { ... }

	// But not for enums or aliases:
	//enum myEnum(T) = T.someValue if (is(typeof(T.someValue))); //NG

	// Instead you're forced to write it in long-form:
	template myEnum(T)
		if (is(typeof(T.someValue)))
		enum myEnum = T.someValue;

Is there any chance of extending the syntax of enums and aliases to also
allow sig constraints, the same way they are allowed in function


"You know, maybe we don't *need* enemies." "Yeah, best friends are about all I
can take." -- Calvin & Hobbes
Jun 26 2014