www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - template specialisation

reply "John C" <johnch_atms hotmail.com> writes:
I don't seem to be able specialise a template in a separate module from the 
original declaration.

    --com.d--

    template DispInterface(T) { ... }

    --shdocvw.d--

    template DispInterface(T : DWebBrowserEvents2) { ... }

    --program.d--

    class WebBrowser : DWebBrowserEvents2 {
        mixin DispInterface!(DWebBrowserEvents2);
    }

I get the following error:
    template com.DispInterface(T) conflicts with shdocvw.DispInterface(T : 
DWebBrowserEvents2)

However, if both template declarations are in the same module, the code 
compiles fine. Is there really a conflict? 
Oct 12 2005
next sibling parent reply "Regan Heath" <regan netwin.co.nz> writes:
On Wed, 12 Oct 2005 11:51:34 +0100, John C <johnch_atms hotmail.com> wrote:
 I don't seem to be able specialise a template in a separate module from  
 the
 original declaration.

     --com.d--

     template DispInterface(T) { ... }

     --shdocvw.d--

     template DispInterface(T : DWebBrowserEvents2) { ... }

     --program.d--

     class WebBrowser : DWebBrowserEvents2 {
         mixin DispInterface!(DWebBrowserEvents2);
     }

 I get the following error:
     template com.DispInterface(T) conflicts with shdocvw.DispInterface(T  
 :
 DWebBrowserEvents2)

 However, if both template declarations are in the same module, the code
 compiles fine. Is there really a conflict?
If you change the mixin to: mixin shdocvw.DispInterface!(DWebBrowserEvents2); I believe the error will vanish. Similarly if you stop including com.d in the program.d file. (At least, in my attempt to replicate your problem these worked) There may be some sort of "alias" line you could add, but I cannot figure it out. It appears that as soon as a matching symbol exists in 2 (or more) scopes it cannot resolve it as it would if they were both in the same scope. I suspect this is a result of D's "simple" overload resolution system. Regan
Oct 12 2005
parent "John C" <johnch_atms hotmail.com> writes:
"Regan Heath" <regan netwin.co.nz> wrote in message 
news:opsyi5abvj23k2f5 nrage.netwin.co.nz...
 On Wed, 12 Oct 2005 11:51:34 +0100, John C <johnch_atms hotmail.com> 
 wrote:
 I don't seem to be able specialise a template in a separate module from 
 the
 original declaration.

     --com.d--

     template DispInterface(T) { ... }

     --shdocvw.d--

     template DispInterface(T : DWebBrowserEvents2) { ... }

     --program.d--

     class WebBrowser : DWebBrowserEvents2 {
         mixin DispInterface!(DWebBrowserEvents2);
     }

 I get the following error:
     template com.DispInterface(T) conflicts with shdocvw.DispInterface(T 
 :
 DWebBrowserEvents2)

 However, if both template declarations are in the same module, the code
 compiles fine. Is there really a conflict?
If you change the mixin to: mixin shdocvw.DispInterface!(DWebBrowserEvents2); I believe the error will vanish. Similarly if you stop including com.d in the program.d file. (At least, in my attempt to replicate your problem these worked)
Thanks for the tip, Regan. That fixed it.
 There may be some sort of "alias" line you could add, but I cannot figure 
 it out.
Adding this line alias shdocvw.DispInterface SHDocVwDispInterface; did the trick too. But I'd rather we didn't have to perform such acrobatics.
 It appears that as soon as a matching symbol exists in 2 (or more) scopes 
 it cannot resolve it as it would if they were both in the same scope. I 
 suspect this is a result of D's "simple" overload resolution system.

 Regan 
Oct 12 2005
prev sibling parent reply xs0 <xs0 xs0.com> writes:
John C wrote:
 I don't seem to be able specialise a template in a separate module from the 
 original declaration.
 
     --com.d--
 
     template DispInterface(T) { ... }
 
     --shdocvw.d--
 
     template DispInterface(T : DWebBrowserEvents2) { ... }
 
     --program.d--
 
     class WebBrowser : DWebBrowserEvents2 {
         mixin DispInterface!(DWebBrowserEvents2);
     }
 
 I get the following error:
     template com.DispInterface(T) conflicts with shdocvw.DispInterface(T : 
 DWebBrowserEvents2)
 
 However, if both template declarations are in the same module, the code 
 compiles fine. Is there really a conflict? 
I think the reason is that they're in different modules, so they're not actually - DispInterface(T) - DispInterface(T : DWebBrowserEvents2) but instead - com.DispInterface(T) - shdocvw.DispInterface(T : DWebBrowserEvents2) So, you're not actually making a specialization, but a completely new template, which causes a conflict.. xs0
Oct 12 2005
parent Sean Kelly <sean f4.ca> writes:
In article <diis6v$14va$1 digitaldaemon.com>, xs0 says...
I think the reason is that they're in different modules, so they're not 
actually

- DispInterface(T)
- DispInterface(T : DWebBrowserEvents2)

but instead

- com.DispInterface(T)
- shdocvw.DispInterface(T : DWebBrowserEvents2)

So, you're not actually making a specialization, but a completely new 
template, which causes a conflict..
I think this is right, though this makes me wonder if using traits templates and similar tricks are possible in D. It's common to require the user to provide specializations in certain cases. I need to find some time to determine if there's a workaround, but it's possible that some means of manual override may be necessary: module a; template doesSomething( T : int ) {} module b; import a; // declare the following as members of module a assign a { template doesSomething( T : char ) {} } // now both of these should work doesSomtething!(int); doesSomething!(char); I'm not sure I like "assign," but that's the general idea. It would be akin to the namespace rules in C++, where it's not uncommon to add overloads to namespace std and the like. This should allow for explicit template overloads without the need to complicate lookup rules. Sean
Oct 12 2005