On 2011-09-29 22:32, dsimcha wrote:
Immutability and type propagation are two of D's greatest assets.  Below is an
example of where they would be difficult to take advantage of:

int foo;
if( bar<  3) {
     try {
         foo = doStuff(someOtherStackVariables);
     } catch(SomeException) {
         foo = 666;
     }
} else {
     foo = 4;
}

I've recently discovered this pattern to allow use of type propagation and
immutability in these situations with no extra boilerplate:

immutable foo = {
     if( bar<  3) {
         try {
             return doStuff(someOtherStackVariables);
         } catch(SomeException) {
             return 666;
         }
     } else {
         return 4;
     }
}();

First, that looks like JavaScript. Second, yet another reason to have a short lambda syntax with auto return.


--
/Jacob Carlborg

Reply via email to