Constrained Templates
Yao G.
nospamyao at gmail.com
Sun Jun 13 13:14:22 PDT 2010
Andrei used that idiom quite extensive in Phobos, unfortunately, I dare to
say. It makes the code a lot harder to read. But the alternative, using
__traits(compiles, ... ) is even worse IMO, because even as is a little
more obvious, is quite verbose.
Something I would like to see with template constraints, is the ability to
show text messages, like when static if is used.
Yao G.
On Sun, 13 Jun 2010 14:36:14 -0500, Leandro Lucarella <llucax at gmail.com>
wrote:
> 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
>
--
Using Opera's revolutionary e-mail client: http://www.opera.com/mail/
More information about the Digitalmars-d
mailing list