Constrained Templates

Leandro Lucarella llucax at gmail.com
Sun Jun 13 12:36:14 PDT 2010


Walter Bright, el 13 de junio a las 12:01 me escribiste:
> http://www.drdobbs.com/blog/archives/2010/06/constrained_tem.html
> 
> Anyone want to do the honors and post to reddit, ycombinator, etc. ?

Nice article, but when I read:

	T gcd(T)(T a, T b)
		if (is(typeof(a % b)))
	{
		...
	}

Under the presence of such beauty as template constraint syntax is,
"is(typeof(a % b))" makes my eyes hurt, and my brain wonder. When you
get used to this idiom, it might not look to bad, but I'm seeing people
new to D wonder "why in the hell was that syntax used?" while they try
to decode what is(typeof()) means.

I think is really a shame, all the beauty and clarity gained by the
simple "if" of template constraints, is lost with the cryptic
is(typeof()) (well, maybe not all, but a significant part).

I would love to see this fixed/simplified. For example, by applying the
changes in suggested in bug 3702 [1]. This looks much better, and is
pretty clear to anyone (even people that doesn't know D):

	T gcd(T)(T a, T b)
		if (meta.compiles(a % b))
	{
		...
	}

I guess at this point this is not going to happen for D2, a real shame
:S

The, the article made me think that, even when template constraints are
useful not only for improving error reporting, they are often used only
for that. Since the compiler knows all the operations a template
parameter will need to satisfy from the function body, it would be nice
to have some sort of way to tell the compiler to write the template
constraints for us (the obvious ones at least, there might be other
template constraints desired besides the ones the ones the compiler can
figure out). This way, the errors can be improved without user
intervention.

[1] http://d.puremagic.com/issues/show_bug.cgi?id=3702

-- 
Leandro Lucarella (AKA luca)                     http://llucax.com.ar/
----------------------------------------------------------------------
GPG Key: 5F5A8D05 (F8CD F9A7 BF00 5431 4145  104C 949E BFB6 5F5A 8D05)
----------------------------------------------------------------------
Salvajes, de traje, me quieren enseñar
Salvajes, de traje, me quieren educar


More information about the Digitalmars-d mailing list