another idea for compile time functions
Yauheni Akhotnikau
eao197 at intervale.ru
Fri Feb 9 00:04:08 PST 2007
On Fri, 09 Feb 2007 08:42:37 +0300, Andrei Alexandrescu (See Website For
Email) <SeeWebsiteForEmail at erdani.org> wrote:
> The attribute is not even needed if meta-code is flagged as such. My
> thoughts currently gravitate around the idea of using mixin as an escape
> into compile-time world. Anything that's printed to standard output in
> compile-time world becomes code, e.g. (using your function):
>
> mixin
> {
> writefln("int x = ", add(10, 20), ";");
> }
>
> is entirely equivalent to:
>
> int x = 30;
>
> No annotation is needed on add because mixin clarifies that it's called
> during compilation. The compiler will complain if add cannot be user
> dually.
>
> Using mixin and write as separation devices makes it very clear what is
> to be done when; otherwise, it quickly becomes confusing what code is
> meant to actually get evaluated eagerly, and what code is to actually be
> "output" for compilation.
This is a good idea!
But I think it is no need to iterpret 'pure D' code at compile time. If
compiler sees such mixin expression it can create temporary D program
which has content of the mixin expression and then execute it in
background with standard output redirection (with help of rdmd).
This approach can be used even with current string mixin expression. For
example, if compiler sees a construct:
mixin( SomeDSLProcessor( `some DSL code` ) );
and knowns than SomeDSLProcessor is an ordinary D function then compiler
can create a simple temporary program:
import <module in which SomeDSLProcessor is defined>;
void main() {
writefln( SomeDSLProcessor( `some DSL code` ) );
}
then run it by rdmd and use its output as argument for mixin expression in
original program.
--
Regards,
Yauheni Akhotnikau
More information about the Digitalmars-d
mailing list