[phobos] A few minor features

David Simcha dsimcha at gmail.com
Sun Oct 31 16:44:47 PDT 2010


On 10/31/2010 7:38 PM, Walter Bright wrote:
>
>
> David Simcha wrote:
>>
>> The functionality is:
>>
>> 1.  Static array literals that are guaranteed to avoid heap allocations:
>>
>> /**
>> Creates a static array literal, avoiding the heap allocation that 
>> results
>> from a regular array literal.
>> */
>> CommonType!(T)[T.length] staticArray(T...)(T elems)
>> if(is(CommonType!(T))) {
>>     typeof(return) ret = void;
>>     foreach(i, Unused; T) {
>>         ret[i] = elems[i];
>>     }
>>
>>     return ret;
>> }
>
> I'd prefer fixing that in the compiler, I just haven't gotten around 
> to it yet.

Ok, sounds good.  I wasn't sure if this was on your todo list or not.
>
>> 3.  An alwaysAssert() function that stays on in release mode but is 
>> otherwise equivalent to assert().  I had been using enforce() for 
>> this, but I don't like that anymore because it throws Exception, not 
>> AssertError by default.  This means that if I write catch(Exception), 
>> I could accidentally be catching what is effectively an assertion 
>> failure.  Since assert() is supposed to be terse to encourage the 
>> programmer to use it liberally, explicitly passing AssertError to 
>> enforce() doesn't cut it.  Mostly what I need here is a better/terser 
>> name than alwaysAssert().  Given the nature of this feature, I'd say 
>> terseness actually beats explicitness.  The best I've come up w/ so 
>> far is rassert() for release assert.
>>
>
>
> So far, what I use for this is:
>
>    if (!condition) assert(0);
>
> Even in release mode, assert(0) will execute a HLT instruction.
>

Andrei pointed that out to me, too.  I think that idiom is a terrible 
solution because:

1.  It's too much typing.  Asserts are something you want sprinkled 
liberally thoughout your codebase, and they should only be removed from 
release builds if they have a non-negligible impact on performance or 
might contain sensitive information that you don't want revealed to 
customers or whatever.  It's absolutely essential that release-mode 
assert not be a PITA to use.

2.  It doesn't give file and line numbers.


More information about the phobos mailing list