what's the point of function template declarations if they can't be defined?

Jonathan M Davis newsgroup.d at jmdavisprog.com
Thu Feb 8 21:52:01 UTC 2018


On Thursday, February 08, 2018 20:16:22 Marc via Digitalmars-d-learn wrote:
> What's a di file? (sorry google didn't help with that)

I"m not sure where the documentation for it is, but it's the D equivalent of
a header file. Basically, it's essentially the same as a .d file except that
it's only imported, never compiled. So, it just contains declarations and
stuff that has to be defined in order to be used when importing (e.g.
templates and anything used during CTFE has to be in the .di file). It's
essentially a stripped down version of the .d file.

So, if a library is going to use a .di file, it declares a .d file as per
normal, and that's compiled into the library, but then a .di file is
distributed with the library instead of a .d file, and that's what a program
using the library would import instead of the .d file. The import itself
doesn't change in any way, but the program then only sees what's declared in
the .di version of the module instead of the .d version.

The whole reason that .di files exist is to provide a way for someone to
distribute a library without distributing the full source (which is
something that companies often like to do). It's a pretty restrictive
feature though, since templates still have to be in the .di file, and if
something isn't defined in the .di file instead of just declared, then it
can't be used with CTFE. And with how much templates and CTFE gets used in a
lot of D code, that tends to mean that either you give up on those features,
or you're forced to put a lot of your implementation in the .di file anyway,
making them kind of useless. So, most of use don't go anywhere near .di
files, and personally, I wish that the feature didn't exist, but it's the
kind of thing that some companies insist on.

- Jonathan M Davis



More information about the Digitalmars-d-learn mailing list