Does D provide separation of interface from implementation in templates?

I haven't tried D yet, but it seems to be a very interesting language that found some neat solutions to problems in C ++. I'm curious, it also allowed to separate the interface from the implementation in the templates? If so, how?

+4
source share
2 answers

no templates used will be fully expanded at compile time

this means that the compiler must know the full code of the template, which makes it impossible to save it from .di files

+7
source

At some point in the processing of template D, all information about the template is required. However, there is no reason why this information should be encoded as source code (OTOH, as an implementation detail, all current D compilers require this). This is the fundamental problem of any language that has patterns that are stronger than generics. The consequences of this depend on what you are trying to do.

If your interest in separating the interface and implementation is to hide the implementation (for example, delivering binary libraries and header files in C), this is not possible. The closest you can get is some kind of code obfuscation system.

If, on the other hand, you are interested in avoiding the cost of processing the templates for each recompilation, something more general, like a binary precompiled header format, can allow the reuse of the results of lexical, syntactic and some passages when compiling several other modules. In fact, this would be easier to do with D than in C.

The third option would be to generate the code for the time code, but this is not much different from the usual connection with the aggressive use of the anologue for precompiled headers.

+2
source

Source: https://habr.com/ru/post/1383290/


All Articles