Question about @nogc
Dominikus Dittes Scherkl via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Tue May 20 13:15:08 PDT 2014
On Tuesday, 20 May 2014 at 17:14:31 UTC, John Colvin wrote:
> On Tuesday, 20 May 2014 at 12:25:11 UTC, Dominikus Dittes
> Scherkl wrote:
>> Did I understand correct that a function can only be @nogc if
>> also all functions that it calls are @nogc too (and of course
>> it doesn't use the GC itself)?
>>
>> If so, should this be possible:
>>
>> string foo()
>> {
>> // use GC to allocate some string
>> }
>>
>> bar @nogc
>> {
>> mixin(foo());
>> }
>>
>> Because, bar() didn't really call foo() but instead foo() is
>> evaluated during compile time and it's result is now part of
>> the code, right?
>
> Yes, that should be allowed.
Thanks. This is nice to know, because I will use this a lot in
the future:
/// create a fixed size array with the given name and with *max*
entries
/// of immutable values of the same type as the return value of
the
/// given function.
/// it contains the values of that function in the range [0..max].
string makeLookupTable(alias fn, uint max=255)(string name) pure
@safe if(is(typeof(fn(max))))
{
string table = "immutable " ~ to!string(typeof(fn(max))) ~ "["
~ to!string(max+1) ~ "] " ~ name ~"= [ ";
foreach(i; 0..max) table ~= to!string(fn(i) ~ ", ";
return table ~ to!string(fn(max) ~" ]";
}
More information about the Digitalmars-d-learn
mailing list