Questions about D

Bill Baxter dnewsgroup at billbaxter.com
Thu May 22 14:15:51 PDT 2008


FireLancer wrote:
> Robert Fraser Wrote:
> 
>> FireLancer wrote:
>>> Ive been learning c++ and was wondering if I should move over to D or not but theres a few details I'm not sure on and I couldn't find a good ansew on.
>>>
>>> 1)Is there a way to "hide" things complelty when building a static libary. eg in c++ if I have:
>>>
>>> //libary.h
>>> double GetRandomNumber();
>>>
>>> //libary.cpp
>>> int DoSomething()
>>> {
>>>  //do something
>>> }
>>> double GetRandomNumber()
>>> {
>>>      return (double)rand() / RAND_MAX;
>>> }
>>>
>>>
>>> This is fine as the user doesn't know DoSomething() exists. But now if they create there own DoSomething() they get linker errors because it exists twice :(
>> Yes and no. In D, everything is qualified by its module name, and 
>> there's a 1:1 module:file correspondence. So it's unlikely that two 
>> modules would have the same fully-qualified name with the exact same symbol.
>>
>> A bigger problem is that D doesn't have the concept of header files 
>> (well, it does sort of with .di files, but they're rarely used in 
>> open-source since they're generally not necessary if you have the source 
>> file). The problem is that if you import one of them, all the symbols 
>> (even those marked "private") will be imported.
> 
> So like in c++ I can't choose not to export something in a static lib and make it completly unavaible?
> 

In C++ you can put things in an anonymous namespace:

namespace {
    int variable_only_i_can_see;
    double function_only_i_can_call() { return (double)rand()/RAND_MAX; }
}

In C you could use "static":

  static int only_i_can_see;

In D I'm not sure if there is any such thing.

--bb


More information about the Digitalmars-d-learn mailing list