A proposal for better template constraint error reporting.

Gor Gyolchanyan gor.f.gyolchanyan at gmail.com
Wed Oct 26 08:34:09 PDT 2011


This idea is still raw, so don't expect me to be able to answer to all
your question. This is just a general direction in which we could move
to improve the error reports, that occur when templates can't be
instantiated.
What if one could add ddoc comments to parts of the constraint, so
that both ddoc generator could output them in a neat way and the
compiler would use them to report errors:

/// Define a state machine transition types.
template transition(ActionType)
	if
	{
		/// Must be a callable type.
		isCallable!ActionType &&
		/// Must return a state.
		is(ReturnType!ActionType : State) &&
		/// Must take exactly two parameters.
		ParameterTypeTuple!(ActionType).length == 2 &&
		/// The first parameter must be an event.
		is(ParameterTypeTuple!(ActionType)[0] : Event) &&
		/// The second parameter must be a state.
		is(ParameterTypeTuple!(ActionType)[1] : State);
	)
{
	alias ParameterTypeTuple!(ActionType)[1] FromState;
	alias ReturnType!ActionType ToState;
	alias ParameterTypeTuple!(ActionType)[0] Event;
}

The ddoc comments, preceding parts of template constraints would be
used to specify why exactly did the template fail to instantiate, for
example:

Error: Cannot instantiate template demo01.transition(ActionType)
because "Must return a state." constraint is not satisfied.
Writing specifications of the template with all different version of
incorrect constraints just to static assert(0) it is too tedious to do
every time.


More information about the Digitalmars-d mailing list