Welcome to Web-News
A Web-based News Reader
Subject Forward declarations of template specilizations.
From Ryan Bloomfield <_sir_maniacREMOVE_ME@yahoo.com>
Date Fri, 14 Nov 2008 15:30:38 -0500
Newsgroups digitalmars.D

I have an interesting issue, that makes me curious on how D handles it.

consider the following:
======================
module List;

template NodeTraits(NodeType)
{
        static NodeType getNext(NodeType v) { return v.next; }
}

class List(NodeType, Traits=NodeTraits!(NodeType))
{
        private NodeType head;

        
        NodeType next() { return Traits.getNext(head); }
}

======================
module main;

class Node {
        Node myNext;
}

template NodeTraits(NodeType : C)
{
        static NodeType getNext(NodeType v) { return v.myNext; }
}

main()
{
            alias List!(Node) L;
        L l = new L();
        C next = l.next(); // error:  no property 'next' for type 'main.Node'
}
==============================================


  Templates are evaluated in the scope they are defined, which makes sense, but when should the compiler check those definitions?  It makes sense that given 2 modules, one module shouldn't be able to change the behavior of another module, having said that, c++ does allow this, and it proves very useful in customizing the behavior of library classes.

  I noticed that if I change it to the following it works correctly(Traits argument becomes an alias, and is passed in main):
======================
module List;

template NodeTraits(NodeType)
{
        static NodeType getNext(NodeType v) { return v.next; }
}

class List(NodeType, alias Traits)
{
        private NodeType head;

        
        NodeType next() { return Traits.getNext(head); }
}

======================
module main;

class Node {
        Node myNext;
}

template NodeTraits(NodeType : Node)
{
        static NodeType getNext(NodeType v) { return v.myNext; }
}

main()
{
       alias NodeTraits!(Node) Traits;  // a template's alias arguments only take a single identifier
            alias List!(Node, Traits) L;
        L l = new L();
        C next = l.next(); // Successfully accesses l.myNext
}
==============================================

Does the evaluation of a template have to know only the scope it was instantiated in?  No forward declaration(of a module that imports it)?

Recent messages in this thread
 
-# Forward declarations of template specilizations. (Current message) Ryan Bloomfield 14-Nov-2008 03:30 pm
.-# Re: Forward declarations of template specilizations. Christian Kamm 16-Nov-2008 03:14 am
..-# Re: Forward declarations of template specilizations. Bill Baxter 16-Nov-2008 04:49 am
..|-# Re: Forward declarations of template specilizations. Ryan Bloomfield 16-Nov-2008 03:52 pm
..|.|# Re: Forward declarations of template specilizations. Frits van Bommel 16-Nov-2008 03:59 pm
..|.\# Re: Forward declarations of template specilizations. Jarrett Billingsley 16-Nov-2008 04:00 pm
..\# Re: Forward declarations of template specilizations. Jarrett Billingsley 16-Nov-2008 11:11 am