The D standard library is built on GC, is that a negative or positive?

Siarhei Siamashka siarhei.siamashka at gmail.com
Thu Dec 15 13:30:57 UTC 2022


On Wednesday, 14 December 2022 at 03:20:13 UTC, Steven 
Schveighoffer wrote:
> On 12/13/22 9:45 PM, Ali Çehreli wrote:
>> On 12/13/22 18:05, H. S. Teoh wrote:
>> 
>>  > Hmm.  Whatever happened to that proposal for GC-less 
>> exceptions?
>>  > Something about allocating the exception from a static 
>> buffer and
>>  > freeing it in the catch block or something?
>> 
>> I have an errornogc module here:
>> 
>>    https://code.dlang.org/packages/alid
>> 
>> I hope it still compiles. :)
>
> FYI, throwing actually uses the GC unless you override the 
> traceinfo allocator. Yes, even if it's marked @nogc (functions 
> marked @nogc can still call arbitrary C functions that might 
> allocate using the GC).

Hmm, I'm experimenting with the following code for errors 
handling in my small @nogc compatible dub package:

```d
@safe @nogc:

/* @nogc compatible replacement for enforce */
T enforce(string msg, T)(T cond)
{
     if (!cond)
     {
         static immutable e = new Exception(msg);
         throw e;
     }
     return cond;
}

void main()
{
     try
     {
         enforce!"trigger exception by a comparison error"(1 == 2);
     }
     catch (Exception e)
     {
         assert(e.msg == "trigger exception by a comparison 
error");
     }
     enforce!"and now again without try/catch"(1 == 2);
}
```

Does it also GC allocate behind the scene? Are there any other 
possible problems with it?


More information about the Digitalmars-d mailing list