www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - void init of out variables

reply Nicholas Wilson <iamthewilsonator hotmail.com> writes:
I have a function that takes a large matrix as an out parameter.
Is there a way to do `=void` for an out parameter like there is 
for is for a plain declaration?
enum M = 2600;
void f() {
     float[M] mean = void; // works as expected, mean is left 
uninitialised
}

void g(out float[M][M] corr) // works but assigns twice
{
     corr[] = float.init; // compiler inserted

     // assign to each value of corr
}

//Error: found ')' when expecting '.' following void
void h(out float[M][M] corr = void)
{

}

is there a way to not assign to out variables?
Aug 18 2017
next sibling parent reply Igor Shirkalin <mathsoft inbox.ru> writes:
On Saturday, 19 August 2017 at 06:20:28 UTC, Nicholas Wilson 
wrote:
 I have a function that takes a large matrix as an out parameter.
 Is there a way to do `=void` for an out parameter like there is 
 for is for a plain declaration?
 enum M = 2600;
 void f() {
     float[M] mean = void; // works as expected, mean is left 
 uninitialised
 }

 void g(out float[M][M] corr) // works but assigns twice
 {
     corr[] = float.init; // compiler inserted

     // assign to each value of corr
 }

 //Error: found ')' when expecting '.' following void
 void h(out float[M][M] corr = void)
 {

 }

 is there a way to not assign to out variables?
Try 'ref' instead of 'out'.
Aug 18 2017
parent reply Nicholas Wilson <iamthewilsonator hotmail.com> writes:
On Saturday, 19 August 2017 at 06:23:10 UTC, Igor Shirkalin wrote:
 On Saturday, 19 August 2017 at 06:20:28 UTC, Nicholas Wilson 
 wrote:
 I have a function that takes a large matrix as an out 
 parameter.
 Is there a way to do `=void` for an out parameter like there 
 is for is for a plain declaration?
 enum M = 2600;
 void f() {
     float[M] mean = void; // works as expected, mean is left 
 uninitialised
 }

 void g(out float[M][M] corr) // works but assigns twice
 {
     corr[] = float.init; // compiler inserted

     // assign to each value of corr
 }

 //Error: found ')' when expecting '.' following void
 void h(out float[M][M] corr = void)
 {

 }

 is there a way to not assign to out variables?
Try 'ref' instead of 'out'.
Hmm, I could, but ref doesn't signal intention like out does.
Aug 18 2017
parent reply Walter Bright <newshound2 digitalmars.com> writes:
On 8/18/2017 11:24 PM, Nicholas Wilson wrote:
 On Saturday, 19 August 2017 at 06:23:10 UTC, Igor Shirkalin wrote:
 On Saturday, 19 August 2017 at 06:20:28 UTC, Nicholas Wilson wrote:
 I have a function that takes a large matrix as an out parameter.
 Is there a way to do `=void` for an out parameter like there is for is for a 
 plain declaration?
 enum M = 2600;
 void f() {
     float[M] mean = void; // works as expected, mean is left uninitialised
 }

 void g(out float[M][M] corr) // works but assigns twice
 {
     corr[] = float.init; // compiler inserted

     // assign to each value of corr
 }

 //Error: found ')' when expecting '.' following void
 void h(out float[M][M] corr = void)
 {

 }

 is there a way to not assign to out variables?
Try 'ref' instead of 'out'.
Hmm, I could, but ref doesn't signal intention like out does.
True. Please file an enhancement request.
Aug 19 2017
parent Nicholas Wilson <iamthewilsonator hotmail.com> writes:
On Saturday, 19 August 2017 at 23:05:26 UTC, Walter Bright wrote:
 On 8/18/2017 11:24 PM, Nicholas Wilson wrote:
 Hmm, I could, but ref doesn't signal intention like out does.
True. Please file an enhancement request.
https://issues.dlang.org/show_bug.cgi?id=17765 I also realised that ref won't error on reads from the parameter, whereas out does.
Aug 19 2017
prev sibling parent kinke <noone nowhere.com> writes:
On Saturday, 19 August 2017 at 06:20:28 UTC, Nicholas Wilson 
wrote:
 is there a way to not assign to out variables?
I don't think so. Is there a good reason not to return the matrix directly (taking advantage of in-place construction)? float[M][M] f() { float[M][M] mean = void; // init return mean; }
Aug 19 2017