Library standardization -> template hiding suggestion !

Edward Diener eddielee_no_spam_here at tropicsoft.com
Sun Apr 20 12:34:00 PDT 2008


Walter Bright wrote:
> Edward Diener wrote:
>> Is there an example of code separated into "headers" and 
>> "implementation" ? I do not realize how this can be done. Can one just 
>> declare a D function or class in a "header" and then import that file 
>> and provide an implementation for what had previously just been 
>> declared ? What about templates ? I do not see how a D template can 
>> just be declared in one .d file and then implemented in another .d file.
>>
>> Where in the documentation are .di files described ? I recall reading 
>> something about .di files somewhere, probably in the spec, but I can 
>> no longer find it.
> 
> All .di files are are files with the function bodies removed, i.e.:
> 
> int foo() { return 3; }
> 
> becomes:
> 
> int foo();

That makes sense !

> 
> There is a compiler switch to generate these files automatically, -H, 
> but they can be done manually.

I see it now in the online compiler documentation.

> 
> As in C++, templates cannot be compiled without their bodies, so the 
> bodies of the templates are not stripped.
> 
> The main difference with C++ is you don't *need* to generate those 
> header files, the compiler is just fine reading the implementation files 
> and extracting what it needs. The .di files are used when desiring to 
> hide the implementation, or for speeding up the compilation.

Good idea !

Now I have a revolutionary suggestion for D made before no doubt in C++. 
I will make it here and then pursue it by Wiki if others are interested.

Not everybody in the world believes that all software must be open 
source. ( gasp ! calls of "oh, no, no" !! People fainting dead away !!! )

There are a few, obviously misguided and benighted souls such as yours 
truly, who actually believe that one should be able to write software 
and sell it on the market, and that to do so one has the actual right of 
not having to distribute the source code, which one created from the 
biblical sweat of one's brow ( or ache of one's fingers, or thought of 
one's mind ).

Doing this in C++ while writing templates is impossible in current 
implementations because the 'export' keyword, which hardly anyone ever 
wanted to implement anyway, except for Daveed Vandevoorde, never 
promises that the separation between template declarations and template 
definitions would enable one only to distribute the template declaration 
without the template definitions. But it was always possible to conceive 
that the template definitions could be "compiled" down into some 
intermediate unreadable format which could become part of the binary 
distribution, ala shared libs, static libs, exes etc.

But since no one in C++ ever thought it important enough to protect the 
template source as an intellectual right which should not need to be 
distributed in easily readable form, no on in C++ ever decided it was 
important enough to standardize the idea of some intermediate 
"compilable" form by which template source could be distributed but 
remain unread by the end user.

Would it be possible that Mr. Walter Bright is sympathetic to the notion 
that template source should be "compilable" down into some sort of 
unreadable format which enables the D compiler to read it but not others 
to discover its source form ? I realize that the very source code which 
might enable D to do this would itself need to be protected from prying 
eyes so that the format of the "compilable" template source could not be 
  easily reverse engineered. Before someone cries that any format can be 
reverse engineered with enough effort, I want merely to say that no 
doubt .lib and .obj files can be reverse engineered to a certain extent 
but at some highly difficult level few, if any, are going to bother. S I 
see no reason why template source code could not be "obfuscated" in a 
similar way.

Do not get me wrong in attempting to believe that I think source should 
never be available. I applaud libraries like Boost and D's own libraries 
where the source is there for anybody to study. But these are not 
implementations being sold to make a direct profit and, even if they 
were, it is certainly the choice of the developer whether or not they 
want to distribute their source code or not.

As a user of libraries largely based on templates, whether C++ or D, 
once I have faith in the quality of the library I feel I have no need to 
look at the source code in order to use it successfully and the actual 
template declarations should be enough, from the user's point of view, 
to interact with the library. Unfortunately in C++ and D this is not the 
case due to the way templates are currently implemented. But I think it 
could be the case if D pursued the line of thought that the end user of 
the template code, as opposed to the compiler itself, really has no need 
to interact with the template definition, as opposed to the template 
declaration, in order to use the template successfully.

And with all that I will just let the sparks fly if they are any, or 
just watch the embers and ashes decompose ( I am in a pseudo-poetic mood 
on this fine spring day where I live ).



More information about the Digitalmars-d mailing list