Optional out parameter of function

Joseph Rushton Wakeling joseph.wakeling at webdrake.net
Tue Nov 26 07:21:33 PST 2013


Suppose that I have an "out" parameter in a function:

     int foo(in int i, out int j)
     {
         j = /* something */;
         return /* whatever */;
     }

Is there any way to mark that out parameter as optional, i.e. so that it only 
gets written to/used if a corresponding parameter is passed to the function?

The goal would be to be able to write e.g.

     int i, j;
     foo(i);     // no need to worry about j
     foo(i, j);  // this time j gets written to

It would be easy to write a wrapper function,

     int foo(in int i)
     {
         int j;
         return foo(i, j);
     }

... but I'm wondering if there is a way to make the out parameter optional 
without doing this.

The use-case I'm thinking of is e.g. for a reporting variable that can be used 
to track some kind of function behaviour, but which one doesn't necessarily 
always want to bother measuring.


More information about the Digitalmars-d-learn mailing list