Wednesday, March 12, 2014

Compile Time Function Execution, a pattern to go by

I've been working quite heavily with Compile Time Function Execution (CTFE) and D's meta programming (templates). Each of these being quite innovative. But paired together they are quite useful.
From generation of code, to handling of UDA's.

I am writing this because, there is a number of bad ways to use templates and CTFE that have a tendency to not only error out but be very hard to understand.
As such I'll be explaining my way of working with it and why.

First off there is two types of code.
  • Helpers
    This code is quite simple. It does one thing. Square a number? Yes sir. Check for some version statements, again yes sir. Is a UDA on a property, another yes sir. But not oh hey I'll call another 10 functions so I can do this thing.
  • Generators
    This code really isn't simple. It calls helper functions a lot. It usually not alone. Now it can be used for to generate code or it can be used to do parsing.
Now with helper functions I have recognized a pattern to what I make in them.
  • Is a specific UDA on it? Simple right? Could be a very nice mixin template.
  • Assuming a specific UDA is on it, get some info from it.
Now you can't really do much with the later, at least without static foreach support. You can write these manually, just not so much via a template mixin.
However the first is easy to do in with a mixin.


There is some unique cases where helper functions are actually utilized via delegate pointers or with function pointers.


This allows you to have runtime registration system that utilises compile time information. Which is a rather nice thing!

These examples really don't do justice to how I work. Better examples is my projects Cmsed and Dvorm. Which uses these formats.

No comments:

Post a Comment