Dependency management in D

ketmar via Digitalmars-d digitalmars-d at puremagic.com
Thu Sep 18 19:05:32 PDT 2014


On Fri, 19 Sep 2014 01:42:58 +0000
Scott Wilson via Digitalmars-d <digitalmars-d at puremagic.com> wrote:

> Do .di files contain only templates (no comments and plain
> functions? How well do they work? thanx
as for 'how .di files work' question: '.di' is just a plain D source,
just with stripped function bodies. nothing very special about that.


so yes, .di files contains only templates and function declarations
(without function bodies). this *can* work, but when it comes to CTFE...

look at the following:

=== z00.d ===
  module z00;
  string foo(string name) { return `int `~name~`() {return 42;}`; }

=== z01.d ===
  import z00;
  mixin(foo(`bar`));
  void main () {
    import std.stdio;
    writeln(bar());
  }

and .di file generated with `dmd -H -c -o- z00.d`:
=== z00.di ===
  // D import file generated from 'z00.d'
  module z00;
  string foo(string name);


do you see any gotchas? heh:
# dmd z01.d
z01.d(2): Error: foo cannot be interpreted at compile time, because it
has no available source code
z01.d(2): Error: argument to mixin must be a string, not (foo("bar"))
of type string

the compiler has no source for foo() anymore, so it can't do CTFE.

you can avoid this by turning foo() into template:
  string foo()(string name) { return `int `~name~`() {return 42;}`; }

but then you should turn all your functions that can be used in CTFE
into templates, and there will be no much sense in .di file anyway.


to make a long story short: don't use .di files unless you *REALLY*
*KNOW* what you're doing. and even then think twice.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 181 bytes
Desc: not available
URL: <http://lists.puremagic.com/pipermail/digitalmars-d/attachments/20140919/4b8daa56/attachment.sig>


More information about the Digitalmars-d mailing list