www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - anyway to debug nogc code with writeln?

reply aliak <something something.com> writes:
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
Sep 01 2018
next sibling parent reply =?UTF-8?Q?Ali_=c3=87ehreli?= <acehreli yahoo.com> writes:
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
Sep 01 2018
parent aliak <something something.com> writes:
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!
Sep 03 2018
prev sibling parent Dennis <dkorpel gmail.com> writes:
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"); ```
Sep 02 2018