www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Import files contains implementation

reply simendsjo <simendsjo gmail.com> writes:
Why does the di files contain implementation?
Is it to allow CTFE from calling modules?

l.d
===
module l;
int f() { return 1; }

l.di
====
// D import file generated from 'l.d'
module l;
int f()
{
return 1;
}

The documentation for this says "They can be used to hide the source 
code", but this is a bit misleading.
http://www.d-programming-language.org/dmd-windows.html#interface_files
Aug 12 2011
parent reply Jonathan M Davis <jmdavisProg gmx.com> writes:
On Friday, August 12, 2011 09:59:05 simendsjo wrote:
 Why does the di files contain implementation?
 Is it to allow CTFE from calling modules?
 
 l.d
 ===
 module l;
 int f() { return 1; }
 
 l.di
 ====
 // D import file generated from 'l.d'
 module l;
 int f()
 {
 return 1;
 }
 
 The documentation for this says "They can be used to hide the source
 code", but this is a bit misleading.
 http://www.d-programming-language.org/dmd-windows.html#interface_files

In order for CTFE to work, you have to have the source of the function. Also, if you want inlining to work, you need the source of the function. You also have to have the source if it's a template. In this case, my guess is that the compiler automatically puts the source in the .di file if its small enough that it expects to inline it, but I don't know. Regardless, the web page isn't really misleading. You're free to get rid of the source if you want to. So, you can use it to hide the source code as much as you want (except for templates, which _have_ to have their source in the .di file). It's just that apparently the compiler doesn't automatically hide all of the source that it could. You can edit them by hand if you want to hide more. But if it hid everything that it could by default, that would negatively impart performance. It's essentially the same situation as C++ header files. You _can_ hide function bodies in the cpp file if you want to (except for templates), but then you don't get inlining. So, you choose what you do and don't put in the header file. It's just that in the case of .di files, there's a tool to automatically generate them instead of forcing you to write the whole thing by hand. - Jonathan M Davis
Aug 12 2011
parent simendsjo <simendsjo gmail.com> writes:
On 12.08.2011 10:11, Jonathan M Davis wrote:
 On Friday, August 12, 2011 09:59:05 simendsjo wrote:
 Why does the di files contain implementation?
 Is it to allow CTFE from calling modules?

 l.d
 ===
 module l;
 int f() { return 1; }

 l.di
 ====
 // D import file generated from 'l.d'
 module l;
 int f()
 {
 return 1;
 }

 The documentation for this says "They can be used to hide the source
 code", but this is a bit misleading.
 http://www.d-programming-language.org/dmd-windows.html#interface_files

In order for CTFE to work, you have to have the source of the function. Also, if you want inlining to work, you need the source of the function. You also have to have the source if it's a template. In this case, my guess is that the compiler automatically puts the source in the .di file if its small enough that it expects to inline it, but I don't know. Regardless, the web page isn't really misleading. You're free to get rid of the source if you want to. So, you can use it to hide the source code as much as you want (except for templates, which _have_ to have their source in the ..di file). It's just that apparently the compiler doesn't automatically hide all of the source that it could. You can edit them by hand if you want to hide more. But if it hid everything that it could by default, that would negatively impart performance. It's essentially the same situation as C++ header files. You _can_ hide function bodies in the cpp file if you want to (except for templates), but then you don't get inlining. So, you choose what you do and don't put in the header file. It's just that in the case of .di files, there's a tool to automatically generate them instead of forcing you to write the whole thing by hand. - Jonathan M Davis

Thanks.
Aug 12 2011