Passing array as const slows down code?

Steven Schveighoffer schveiguy at yahoo.com
Fri Apr 27 11:39:02 PDT 2012


On Fri, 27 Apr 2012 14:29:40 -0400, Joseph Rushton Wakeling  
<joseph.wakeling at webdrake.net> wrote:

> On 27/04/12 20:25, H. S. Teoh wrote:
>> On Fri, Apr 27, 2012 at 07:25:30PM +0200, Joseph Rushton Wakeling wrote:
>>> I was more concerned that the compiler wasn't identifying what to me
>>> was a violation of purity.  I'm fairly sure I can also find a way to
>>> make some of those "nothrow" functions throw an error ...
>>
>> It's not a violation of purity, it's just "weak purity". If you try to
>> access a global variable, for example, it will trigger an error.
>
> Thanks for the extended description of weak purity -- it's been very  
> helpful in understanding the concept better.
>
> Is there a particular way in which I can explicitly mark a function as  
> strongly pure?

No, just make sure all the parameters and the result are either immutable  
or implicitly castable to immutable (hard to explain this better).

Hm... gives me a thought that unit tests should have a helper that allows  
ensuring this:

static assert(isStrongPure!fn);

Or maybe __traits(isStrongPure, fn) if it's too difficult to do in a  
library.

> ... yes, as I just found out when I decided to test it 2 minutes ago  
> :-)  OTOH I found that with or without the nothrow option, when the  
> -release flag was used in compiling the code, the error was not thrown  
> and the program did not exit -- it just sat there seemingly running but  
> doing nothing.  This was unexpected ...
>
> The deliberate error was in this case a range exception when accessing  
> an array.

array bounds checks and asserts are turned off during -release compilation  
:)

-Steve


More information about the Digitalmars-d-learn mailing list