Thread overview | ||||||
---|---|---|---|---|---|---|
|
September 01, 2018 anyway to debug nogc code with writeln? | ||||
---|---|---|---|---|
| ||||
I would like to debug a few things and need to insert print statements to figure things out. I thought that using debug print would be ok in nogc code? Seems it make the compiler infer f as not nogc though so this is basically unworkable unless I go through my entire source code and remove the @nogc attribute wherever function f is used. Anyway around this? Here's example code: import std.stdio; void f(T)(auto ref T) { debug writeln("yo"); } @nogc void main() { f(3); } Error: @nogc function D main cannot call non-@nogc function onlineapp.f!int.f Cheers, - Ali |
September 01, 2018 Re: anyway to debug nogc code with writeln? | ||||
---|---|---|---|---|
| ||||
Posted in reply to aliak | You can strip off any attribute with SetFunctionAttributes: import std.stdio; // Adapted from std.traits.SetFunctionAttributes documentation import std.traits; auto assumeNoGC(T)(T t) if (isFunctionPointer!T || isDelegate!T) { enum attrs = functionAttributes!T | FunctionAttribute.nogc; return cast(SetFunctionAttributes!(T, functionLinkage!T, attrs)) t; } void f(T)(auto ref T) { writeln("yo"); } @nogc void main() { assumeNoGC(() => f(3)); // or assumeNoGC( { writeln("yo"); }); } Ali |
September 02, 2018 Re: anyway to debug nogc code with writeln? | ||||
---|---|---|---|---|
| ||||
Posted in reply to aliak | On Saturday, 1 September 2018 at 21:53:03 UTC, aliak wrote:
> Anyway around this?
I don't know if your situation allows it, but you can mark f explicitly as always @nogc. If your design assumes that it's @nogc, it's a good idea to add the attribute anyway.
You can also use the C printf function:
```
import core.stdc.stdio: printf;
printf("yo\n");
```
|
September 03, 2018 Re: anyway to debug nogc code with writeln? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Ali Çehreli | On Saturday, 1 September 2018 at 22:38:46 UTC, Ali Çehreli wrote:
> You can strip off any attribute with SetFunctionAttributes:
>
> import std.stdio;
>
> // Adapted from std.traits.SetFunctionAttributes documentation
> import std.traits;
> auto assumeNoGC(T)(T t)
> if (isFunctionPointer!T || isDelegate!T)
> {
> enum attrs = functionAttributes!T | FunctionAttribute.nogc;
> return cast(SetFunctionAttributes!(T, functionLinkage!T, attrs)) t;
> }
>
> void f(T)(auto ref T) {
> writeln("yo");
> }
>
> @nogc void main() {
> assumeNoGC(() => f(3));
> // or
> assumeNoGC( { writeln("yo"); });
> }
>
> Ali
Ah this works! Can define a debugWriteln then that can be used from anywhere without having to re attribute all the functions.
Thanks!
|
Copyright © 1999-2021 by the D Language Foundation