digitalmars.D - Adding concepts to D
- Sean Kelly (39/39) Jan 17 2006 Currently, to specialize on a concept, something like the following must...
Currently, to specialize on a concept, something like the following must be done: template isDog( T ) { const bit isDog = is(T:Scottie) || is(T:Daschund); } template walkAnimal( T, bit check : true = isDog!( T ) ) { walkAnimal( T animal ) { // make sure the user didn't try to trick me static assert( isDog!( T ) ); getDoggieBiscuits(); walkDog(); } } template walkAnimal( T, bit check : false ) { walkAnimal( T animal ) { // make sure the user didn't try to trick me static assert( !isDog!( T ) ); putOnRunningShoes(); getBigStick(); walkNastyCritter(); } } This is a bit awkward for simple cases and can easily become quite complex. Also, it is unappealing that the concept checking currently must be done via template parameters, which can be overridden by the clever user. C++ 0x will support concepts in some form both to aid in error reporting and to simplify trait-based specialization, and it would be tremendously useful for D to have something similar. Also, I think D is already is very good shape for supporting this, as static if, is, etc, provide an existing, straightforward means to switch on type attributes. The C++ proposals are outlined in various forms here: http://www.open-std.org/JTC1/SC22/WG21/docs/papers/2005/n1899.pdf http://www.open-std.org/JTC1/SC22/WG21/docs/papers/2005/n1886.pdf http://www.open-std.org/JTC1/SC22/WG21/docs/papers/2005/n1849.pdf The key concept in each proposal is a 'where' clause that is separated from the list of template parameters. I won't pretend to know how this would best fit into the D syntax however. Sean
Jan 17 2006