Question about function type parameter type hints?

Dicebot public at dicebot.lv
Mon Jul 15 12:18:11 PDT 2013


On Monday, 15 July 2013 at 18:56:38 UTC, Gary Willoughby wrote:
> Are the two above class declarations achieving the same thing? 
> i.e. is the type hint of the second snippet shorthand for the 
> first's 'if'? If so which is preferred?

No, ":" stands for "same or implicitly convertible" while "==" is 
strict "same":

T foo1(T : int)()
{
	return T.init;
}

T foo2(T)()
	if (is(T == int))
{
	return T.init;
}

void main()
{
	foo1!short();
	foo2!short(); // error
}

As far as I know there are no "==" syntax for template 
specialization. Usually template specialization syntax is 
preferable for simple cases because it is more readable but any 
complex case pretty much requires "if" constraint.


More information about the Digitalmars-d-learn mailing list