Template Interface
Nick Treleaven
nospam at example.net
Wed Jun 13 06:34:25 PDT 2012
On 12/06/2012 18:56, Nathan M. Swan wrote:
> When writing a generic function which takes an unknown type, the
> signature is written like so:
>
> void fun(L)(L l) if (isList!L);
>
> While writing a generic interface is written like so:
>
> template isList(L) {
> enum bool isList = is(typeof(
> (inout int _dummy=0)
The above line seems unnecessary as we have {no_argument_lambda;} syntax
- or is there a special reason why?
> {
> L l;
> if (l.nil) {}
> auto e = l.car;
> l = l.cdr;
> ));
> }
BTW I think Walter said we could have enum template syntax, which would
improve the above a little:
enum bool isList(L) = is(typeof(...
> This doesn't seem very intuitive to me. OOP languages handle this with
> interfaces, but in D for things like ranges we often use structs, making
> it incompatible with the current "interface."
A possible enhancement is to allow structs to implement interfaces (but
this has a runtime cost, unlike using templates).
> I'm suggesting something like a "template interface", which is
> compatible with all types, and serves as a placeholder for any type for
> which the body compiles:
[snip]
> void fun(List!string l);
Maybe:
void fun(L:IList)(L l);
template interface IList(E) {
> template interface List(E) : List {
> List!E l;
> E e = l.car;
> }
>
> It makes writing generic code much cleaner.
I'm not quite sure what the ': List' part means - is it just to declare
that the List struct is used below?
Maybe it would be better for the template interface body to contain
method signatures/members rather than code?
I think it might be an interesting idea, but it would probably need to
be significantly better than the current way with constraints to get
adopted by D.
Nick
More information about the Digitalmars-d
mailing list