Delegate Literals + Immutability, pure and closures
Jacob Carlborg
doob at me.com
Thu Sep 29 23:32:44 PDT 2011
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
More information about the Digitalmars-d
mailing list