www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - public imports and template functions

reply Jonathan M Davis <jmdavisProg gmx.com> writes:
Am I correct in my understanding that if you wish a template function which is 
imported from another module to compile correctly without requiring other 
imports in the module that your using the function in that the module with the 
template function needs to publically import all of the functions and types
that 
it needs?

So, if you had

----------------------
module a;

import std.algorithm;

void func(R)(R range)
{
    sort(range);
}

--------------------------
module b;

import module a;

void foobar(int[] vals)
{
    sort(vals);
}
----------------------------------


then you'd have to import std.algorithm (or std.algorithm.sort) in module b,
and 
if you didn't want to require that, you'd do

public import std.algorithm : sort;

in module a. Is that correct? Or is my understanding off?

- Jonathan M Davis
Nov 24 2010
next sibling parent Don <nospam nospam.com> writes:
Jonathan M Davis wrote:
 Am I correct in my understanding that if you wish a template function which is 
 imported from another module to compile correctly without requiring other 
 imports in the module that your using the function in that the module with the 
 template function needs to publically import all of the functions and types
that 
 it needs?

That's only true of mixins. (And for that reason, you should generally use fully qualified names in mixins).
Nov 24 2010
prev sibling parent Jonathan M Davis <jmdavisProg gmx.com> writes:
On Wednesday 24 November 2010 00:18:44 Don wrote:
 Jonathan M Davis wrote:
 Am I correct in my understanding that if you wish a template function
 which is imported from another module to compile correctly without
 requiring other imports in the module that your using the function in
 that the module with the template function needs to publically import
 all of the functions and types that it needs?

That's only true of mixins. (And for that reason, you should generally use fully qualified names in mixins).

That makes sense (and IMHO is much better behavior that what I was thinking the case was). I usually have so many imports which are common between files that it isn't an issue even with mixins, but it's a good idea to try and get rid of imports that you're not using, and in order to do that properly, you have to understand the import rules. I was afraid that some of my template stuff was working only because so many of the imports were common between files, but fortunately, that's not the case. Thanks for the clarification. - Jonathan M Davis
Nov 24 2010