Optional extra return value? Multiple return values with auto?

Chris NS ibisbasenji at gmail.com
Tue Jul 24 00:47:11 PDT 2012


On Tuesday, 24 July 2012 at 03:25:55 UTC, ReneSac wrote:
>
> Do I really have to duplicate the function, in order to achieve 
> this?
>

In a nutshell, yes.  Or else resort to bizarre sorcery such as 
may rot the very heart from one's chest (or template ninjitsu, 
whatever).  But is it really so bad?

bool foo ( byte[] data, out int stats ) {
     // do a bunch of stuff
}

bool foo ( byte[] data ) {
     int dummy;
     return foo( data, dummy );
}

One could possibly put together a template that automates this... 
heck, here's a quick and dirty implementation of such:

##############################
import  std.stdio   ,
         std.traits  ;

template DummyLast ( alias Func ) {
     ReturnType!Func DummyLast ( ParameterTypeTuple!Func[ 0 .. $ - 
1 ] args ) {
         ParameterTypeTuple!Func[ $ - 1 ] dummy;
         return Func( args, dummy );
     }
}

bool foo ( byte[] data, out int stats ) {
     stats = 42;
     return true;
}

alias DummyLast!foo foo;

void main () {
     byte[] data;
     bool result;
     int stats;
     result = foo( data, stats );
     writeln( result, ' ', stats );

     result = false;
     stats  = 0;
     result = foo( data );
     writeln( result, ' ', stats );
}
##############################

-- Chris NS



More information about the Digitalmars-d-learn mailing list