Thread overview | ||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
August 30, 2016 Debug prints in @nogc | ||||
---|---|---|---|---|
| ||||
I'm struggling with debug printing in my @nogc-containers. The alternatives: assert(false, "Fixed message with no parameters"); is not enough for my needs debug writeln("Fixed"); doesn't bypass @nogc checking. Why? And temporary commenting out @nogc is cumbersome. I'm aware of C-style printf but that is not as flexible as writeln. Any advice? |
August 30, 2016 Re: Debug prints in @nogc | ||||
---|---|---|---|---|
| ||||
Posted in reply to Nordlöw | On Tuesday, 30 August 2016 at 10:26:28 UTC, Nordlöw wrote:
> I'm struggling with debug printing in my @nogc-containers.
>
> The alternatives:
>
> assert(false, "Fixed message with no parameters");
>
> is not enough for my needs
>
> debug writeln("Fixed");
>
> doesn't bypass @nogc checking. Why?
>
> And temporary commenting out @nogc is cumbersome.
>
> I'm aware of C-style printf but that is not as flexible as writeln.
>
> Any advice?
I have experienced the same pain, but afaik there's no good solution except commenting or printf and I really wish the debug directive would ignore all attribute restrictions and not only pure.
To my knowledge the sole good solution is to fix DMD / D.
|
August 30, 2016 Re: Debug prints in @nogc | ||||
---|---|---|---|---|
| ||||
Posted in reply to Nordlöw | On 30/08/2016 10:26 PM, Nordlöw wrote:
> I'm struggling with debug printing in my @nogc-containers.
>
> The alternatives:
>
> assert(false, "Fixed message with no parameters");
>
> is not enough for my needs
>
> debug writeln("Fixed");
>
> doesn't bypass @nogc checking. Why?
>
> And temporary commenting out @nogc is cumbersome.
>
> I'm aware of C-style printf but that is not as flexible as writeln.
>
> Any advice?
Static array + formattedWrite, perhaps?
|
August 30, 2016 Re: Debug prints in @nogc | ||||
---|---|---|---|---|
| ||||
Posted in reply to rikki cattermole | On Tuesday, 30 August 2016 at 10:36:13 UTC, rikki cattermole wrote: > On 30/08/2016 10:26 PM, Nordlöw wrote: >> I'm struggling with debug printing in my @nogc-containers. >> >> The alternatives: >> >> assert(false, "Fixed message with no parameters"); >> >> is not enough for my needs >> >> debug writeln("Fixed"); >> >> doesn't bypass @nogc checking. Why? >> >> And temporary commenting out @nogc is cumbersome. >> >> I'm aware of C-style printf but that is not as flexible as writeln. >> >> Any advice? > > Static array + formattedWrite, perhaps? I can't grasp how to use this from the docs at https://dlang.org/library/std/format/formatted_write.html uint fwrite(A...)(A a) { import std.stdio : LockingTextWriter; import std.stdio : stdout; import std.format : formattedWrite; return formattedWrite!(stdout.lockingTextWriter)(a); } LockingTextWriter is not publicly exported from std.stdio and stdout.lockingTextWriter can not be read at compile-time. Help please. |
August 30, 2016 Re: Debug prints in @nogc | ||||
---|---|---|---|---|
| ||||
Posted in reply to Nordlöw | On 30/08/2016 11:10 PM, Nordlöw wrote: > On Tuesday, 30 August 2016 at 10:36:13 UTC, rikki cattermole wrote: >> On 30/08/2016 10:26 PM, Nordlöw wrote: >>> I'm struggling with debug printing in my @nogc-containers. >>> >>> The alternatives: >>> >>> assert(false, "Fixed message with no parameters"); >>> >>> is not enough for my needs >>> >>> debug writeln("Fixed"); >>> >>> doesn't bypass @nogc checking. Why? >>> >>> And temporary commenting out @nogc is cumbersome. >>> >>> I'm aware of C-style printf but that is not as flexible as writeln. >>> >>> Any advice? >> >> Static array + formattedWrite, perhaps? > > I can't grasp how to use this from the docs at > > https://dlang.org/library/std/format/formatted_write.html > > uint fwrite(A...)(A a) > { > import std.stdio : LockingTextWriter; > import std.stdio : stdout; > import std.format : formattedWrite; > return formattedWrite!(stdout.lockingTextWriter)(a); > } > > LockingTextWriter is not publicly exported from std.stdio and > stdout.lockingTextWriter can not be read at compile-time. > > Help please. I fixed your code but lockingTextWriter is not @nogc. void main() @nogc { import std.stdio : stdout; import std.format : formattedWrite; formattedWrite(stdout.lockingTextWriter, "%x", 7); } Okay looks like formattedWrite isn't @nogc able. Oh wells thought it was. Please create an issue as this is quite an important feature. |
August 30, 2016 Re: Debug prints in @nogc | ||||
---|---|---|---|---|
| ||||
Posted in reply to Nordlöw | Am Tue, 30 Aug 2016 10:26:28 +0000 schrieb Nordlöw <per.nordlow@gmail.com>: > I'm struggling with debug printing in my @nogc-containers. > > The alternatives: > > assert(false, "Fixed message with no parameters"); > > is not enough for my needs > > debug writeln("Fixed"); > > doesn't bypass @nogc checking. Why? > > And temporary commenting out @nogc is cumbersome. > > I'm aware of C-style printf but that is not as flexible as writeln. > > Any advice? ----------------------------------------------------- import std.stdio; debug { enum writelnPtr = &writeln!string; enum void function(string) @nogc writelnNoGC = cast(void function(string) @nogc)writelnPtr; } void main() @nogc { debug writelnNoGC("foo"); } ----------------------------------------------------- As long as it's only for debugging, the extra indirection shouldn't matter for performance. Even for release builds the optimizer can probably remove the indirection. An alternative solution is using mangleof + pragma(mangle) to refer to the external function. In both cases this approach can be tedious for templated methods. You don't want to write that boilerplate for every possible type combination. However, it should be possible to refactor the code above into a template and automate the boilerplate generation in some way. |
August 30, 2016 Re: Debug prints in @nogc | ||||
---|---|---|---|---|
| ||||
Posted in reply to Johannes Pfau | On Tuesday, 30 August 2016 at 11:52:21 UTC, Johannes Pfau wrote:
> import std.stdio;
>
> debug
> {
> enum writelnPtr = &writeln!string;
> enum void function(string) @nogc writelnNoGC =
> cast(void function(string) @nogc)writelnPtr;
> }
>
> void main() @nogc
> {
> debug writelnNoGC("foo");
> }
Just being able to print a string is not good enough. I want the variadic part writeln so I can debug-print values in my buggy code. Do you have a similar solution?
|
August 30, 2016 Re: Debug prints in @nogc | ||||
---|---|---|---|---|
| ||||
Posted in reply to Nordlöw | On Tuesday, 30 August 2016 at 14:38:47 UTC, Nordlöw wrote: > Just being able to print a string is not good enough. I want the variadic part writeln so I can debug-print values in my buggy code. Do you have a similar solution? Take a look at the example here: http://dlang.org/phobos/std_traits#SetFunctionAttributes You could make a `assumeNogc` template similar to the example `assumePure`. Oh yeah, here's one I prepared earlier: https://dpaste.dzfl.pl/254a5c2697a7 |
August 30, 2016 Re: Debug prints in @nogc | ||||
---|---|---|---|---|
| ||||
Posted in reply to Cauterite | Am Tue, 30 Aug 2016 16:37:53 +0000 schrieb Cauterite <cauterite@gmail.com>: > On Tuesday, 30 August 2016 at 14:38:47 UTC, Nordlöw wrote: > > Just being able to print a string is not good enough. I want the variadic part writeln so I can debug-print values in my buggy code. Do you have a similar solution? > > Take a look at the example here: > http://dlang.org/phobos/std_traits#SetFunctionAttributes > You could make a `assumeNogc` template similar to the example > `assumePure`. > > Oh yeah, here's one I prepared earlier: https://dpaste.dzfl.pl/254a5c2697a7 Nice! Here's a slightly modified version: https://dpaste.dzfl.pl/8c5ec90c5b39 This version does not need an additional delegate. It can be used like this: assumeNogc!writefln("foo %s", 42); assumeNogc!writeln("foo", 42); |
August 30, 2016 Re: Debug prints in @nogc | ||||
---|---|---|---|---|
| ||||
Posted in reply to Johannes Pfau | On Tuesday, 30 August 2016 at 17:11:48 UTC, Johannes Pfau wrote: > Nice! Here's a slightly modified version: https://dpaste.dzfl.pl/8c5ec90c5b39 > > This version does not need an additional delegate. It can be used like this: > > assumeNogc!writefln("foo %s", 42); > assumeNogc!writeln("foo", 42); Marvellous! I update my cryptic but short-and-sweet `dln` at https://github.com/nordlow/phobos-next/blob/master/src/dbg.d#L58 I would love to see this very useful little snippet make its way into Phobos somehow. Any suggestions on how? |
Copyright © 1999-2021 by the D Language Foundation