Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compile Time Code Generation in D

I'm currently learning D, and one of the things I've wondered about is whether D has a way of duplicating the code generation capabilities of JVM languages.

Here's a scenario: I have an object, and I want to generate a string based on that object that gives it's name and all its fields. In Java/Scala, I could just use reflection, but suppose speed is an issue. I could use a bytecode generation library to dynamically generate and compile a class that does this without reflection. The implementation would break down to iterating through the object's fields and getting it's name through recursion, and using that information to dynamically generate Java (or bytecode) that accesses the fields directly.

If you don't like that scenario because it's weak and/or unrealistic, another one that might be more realistic is optimized object serialization.

I've seen examples where D's compile time evaluation and/or template metaprogramming is used for things like precalculating the fibonacci sequence at compile time and other recursive algorithms, but is there a way of doing things like this with just the language and a compiler, or would you need to develop a separate code-generator and run it before the compiler to get this sort of functionality?

like image 869
debio Avatar asked Sep 10 '10 06:09

debio


People also ask

What is compile time code?

Compile time is the period when the programming code (such as C#, Java, C, Python) is converted to the machine code (i.e. binary code). Runtime is the period of time when a program is running and generally occurs after compile time.

What happens at compile time in C?

Compile-time is the period when the programming code is converted to the machine code. Compile-time errors are the errors that are encountered at the time of compilation of the program. They are Syntax errors and Semantic errors. Syntax errors are the compile-time errors that occur due to the use of the wrong syntax.

What is code generation in compiler design?

In computing, code generation is part of the process chain of a compiler and converts intermediate representation of source code into a form (e.g., machine code) that can be readily executed by the target system. Sophisticated compilers typically perform multiple passes over various intermediate forms.

What is compile time and execution time?

Compile-time is the time at which the source code is converted into an executable code while the run time is the time at which the executable code is started running. Both the compile-time and runtime refer to different types of error.


1 Answers

Not only can this be done, it's practically done for you in D2. All you need is a small mixin to turn these from compile time features into runtime features.

  • For the class name, all you need to do is evaluate typeof(this).stringof inside the class's scope.

  • For a list of all fields, try __traits(allMembers, typeof(this)) and then filter out the stuff that's not a field (std.traits will be useful here).

like image 101
dsimcha Avatar answered Oct 23 '22 02:10

dsimcha