[phobos] A few minor features

David Simcha dsimcha at gmail.com
Sun Oct 31 08:54:18 PDT 2010


I want to add a few minor pieces of functionality to Phobos, but would 
like some feedback on:

1.  Whether they're necessary or whether some more general feature will 
eventually solve the same problem.

2.  What module they belong in.

3.  Better names.  I want very terse names for all of these because 
their nature is to work around issues (not bugs, just design decisions 
that get in the way in a few specific cases) with builtin language 
features that are very terse.

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;
}

2.  A function that allows the address of a nested function to be taken 
w/o a heap allocation.

/**
Allows the address of a nested function to be (unsafely) taken without
resulting in a heap allocation.
*/
T scopeAddress(T)(scope T val) {
     return val;
}

Usage:

void fun() {
      void nested() {}

     auto addr = scopeAddress(&nested);
}

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.


More information about the phobos mailing list