www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Compiling debug missing errors

reply Duarte <not-a-realy-address gmx.com> writes:
Hi all,

Using the following example:

---------------

import std.stdio;

pure void foo()
{
     debug
     {
         stdout.writeln("1");
     }

     stdout.writeln("2");
}

void main(string[] args)
{
     foo();
}

---------------

Using either '-debug' or '-release', the second stdout will give 
an error (Error: pure function 'main.foo' cannot access mutable 
static data 'stdout') which is alright by me.

The question is why the first stdout doesn't throw the same error 
when compiling with the '-debug' option? Surely the issue will be 
the same and the debug-statement should be satisfied.


Ĝis,
Duarte
Apr 09
parent reply ag0aep6g <anonymous example.com> writes:
On 04/09/2017 10:49 PM, Duarte wrote:
 ---------------

 import std.stdio;

 pure void foo()
 {
     debug
     {
         stdout.writeln("1");
     }

     stdout.writeln("2");
 }

 void main(string[] args)
 {
     foo();
 }

 ---------------

 Using either '-debug' or '-release', the second stdout will give an
 error (Error: pure function 'main.foo' cannot access mutable static data
 'stdout') which is alright by me.

 The question is why the first stdout doesn't throw the same error when
 compiling with the '-debug' option? Surely the issue will be the same
 and the debug-statement should be satisfied.
You're allowed to break purity in debug code. From the spec: "As a concession to practicality, a pure function can also [...] perform impure operations in statements that are in a ConditionalStatement controlled by a DebugCondition" https://dlang.org/spec/function.html#pure-functions
Apr 09
parent Duarte <not-a-realy-address gmx.com> writes:
On Sunday, 9 April 2017 at 20:59:26 UTC, ag0aep6g wrote:
 On 04/09/2017 10:49 PM, Duarte wrote:
 [...]
You're allowed to break purity in debug code. From the spec: "As a concession to practicality, a pure function can also [...] perform impure operations in statements that are in a ConditionalStatement controlled by a DebugCondition" https://dlang.org/spec/function.html#pure-functions
Cheers mate!
Apr 09