@nogc and Phobos

rempas rempas at tutanota.com
Sat Mar 11 13:18:13 UTC 2023


On Saturday, 11 March 2023 at 12:04:25 UTC, bomat wrote:

Hello! First of all, let's start by making clear of what `@nogc` 
means. It it an attribute that is used one function signatures 
and it annotates that the garbage collector will not be used 
inside this function. However, in the scenario that we have 
`func1` that is annotated with `@nogc` and `func2` that is called 
by `func1`, I don't know if `func2` needs to be annotated with 
`@nogc` as well. This is not been said inside the SPEC (or at 
least, I didn't found it with the quick search I did) so someone 
could confirm or deny this if they know.

> It works fairly well so far, just one problem: Dear ImGui is 
> obviously `@nogc`, and I noticed that it doesn't mix with 
> standard library functions.

Is it what people say "100% `@nogc`" which means that it doesn't 
use the garbage collector or does it use 
[betterC](https://dlang.org/spec/betterc.html)?

> So my question is:
> Is Phobos essentially incompatible to `@nogc`?

Look [here](https://dlang.org/spec/function.html#nogc-functions) 
about the features that cannot be used with `@nogc`. Keep in mind 
that using `@nogc` in your project, will still have it link with 
Phobos and D's runtime library. `BetterC` is what disables the 
garbage collector completely (so no need for `@nogc`) but also 
doesn't make your binary/library link with D's runtime and 
Phobos. But if you don't use this mode, you will be able to link 
with the standard library and use the functions that do not use 
the garbage collector.

> Or is there a trick for mixing GC code with non-GC code that I 
> don't know?

For what I know, you can combine both methods by using the GC and 
also mannually allocating memory with libc's "malloc" (or of 
course your own allocator) but you'll probably have to look into 
some documentation on how to combine them without any sneaky 
bugs. However, you will probably not have to do anything 
"special" to make it work. As I never did it, maybe someone else 
could help on that one.

>
> I'm assuming the second, for if the first was true I'd say that 
> D would be pretty much useless when combined with non-D libs...

Even if the first was the case (which again, only things that 
need the GC will not be able to be used), D is far from been 
"useless". You can use `betterC` and use C's libraries. 
Personally, that's what I'm doing anyway. Even if there was no 
garbage collector and a runtime, I still don't like Phobos and 
the built-in structures of D. It is still far far better than 
pretty much any other (compiled) language out there in case of 
usability so it's not useless at all even if you cannot use the 
standard library!



More information about the Digitalmars-d-learn mailing list