Aliasing a mixin (or alternative ways to profile a scope)

Johannes Loher johannesloher at fg4f.de
Thu Mar 7 21:50:17 UTC 2019


Am 07.03.19 um 22:21 schrieb Simon:
> 
> Is there a way to achieve this while compiling with -betterC? I use a
> custom string struct right now, and your version needs TypeInfo,
> concatening using ~ needs the garbage collector. I have the feeling D is
> really not agreeing with the way I want to do things. If this is not
> possible, I will just roll with the Adam's struct solution.
> 

Using Adams struct solution is perfectly fine. I believe it is probably
the cleaner solution overall.

My solution depends on TypeInfo because format does. I used format
because it makes the string more readable. You can avoid this and use
string concatenation in combination with providing the function name as
template parameter instead:

```
enum profile_scope(string name) = "import core.stdc.stdio : printf;
printf(\""
    ~ name ~ "\n\"); scope(exit) printf(\"" ~ name ~ "\n\");";

extern (C) void main()
{
    mixin(profile_scope!"func1");
}

```
This uses string concatenation only at compile time and not during run
time, so it does not require the garbage collector and is compatible
with betterC :)


More information about the Digitalmars-d-learn mailing list