www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - identic ref/inout parameter

reply Wilhelm <Wilhelm.Kopenpart t-online.de> writes:
Who do I determine in a function (probably with a assert statement) that the
function with more the one ref/inout parameters of the same type are not called
with the same variable like:

private import std.stdio;

int main(char[][] args)
{
    int i=3;
    test( i,i,i,i);
    return 0;
}


void test(ref int r1, ref int r2, inout int i1, inout int i2)
{
	writefln ("           Ref: r1=%d r2=%d", r1, r2);
	writefln ("         InOut: i1=%d i2=%d", i1, i2);
	r1 += 2;
	writefln ("r1 += 2;   Ref: r1=%d r2=%d", r1, r2);
	writefln ("r1 += 2; InOut: i1=%d i2=%d", i1, i2);
	i2 += 2;
	writefln ("i1 += 2;   Ref: r1=%d r2=%d", r1, r2);
	writefln ("i1 += 2; InOut: i1=%d i2=%d", i1, i2);
}

!! That may cause interesting results !!
Sep 14 2007
parent reply Regan Heath <regan netmail.co.nz> writes:
Wilhelm wrote:
 Who do I determine in a function (probably with a assert statement) that the
function with more the one ref/inout parameters of the same type are not called
with the same variable like:
 
 private import std.stdio;
 
 int main(char[][] args)
 {
     int i=3;
     test( i,i,i,i);
     return 0;
 }
 
 
 void test(ref int r1, ref int r2, inout int i1, inout int i2)
 {
 	writefln ("           Ref: r1=%d r2=%d", r1, r2);
 	writefln ("         InOut: i1=%d i2=%d", i1, i2);
 	r1 += 2;
 	writefln ("r1 += 2;   Ref: r1=%d r2=%d", r1, r2);
 	writefln ("r1 += 2; InOut: i1=%d i2=%d", i1, i2);
 	i2 += 2;
 	writefln ("i1 += 2;   Ref: r1=%d r2=%d", r1, r2);
 	writefln ("i1 += 2; InOut: i1=%d i2=%d", i1, i2);
 }
 
 !! That may cause interesting results !!

Maybe... assert(&r1 != &r2); assert(&r1 != &i1); assert(&r1 != &i2); assert(&r2 != &i1); assert(&r2 != &i2); assert(&i1 != &i2); Regan
Sep 14 2007
next sibling parent Regan Heath <regan netmail.co.nz> writes:
Regan Heath wrote:
 Wilhelm wrote:
 Who do I determine in a function (probably with a assert statement) 
 that the function with more the one ref/inout parameters of the same 
 type are not called with the same variable like:

 private import std.stdio;

 int main(char[][] args)
 {
     int i=3;
     test( i,i,i,i);
     return 0;
 }


 void test(ref int r1, ref int r2, inout int i1, inout int i2)
 {
     writefln ("           Ref: r1=%d r2=%d", r1, r2);
     writefln ("         InOut: i1=%d i2=%d", i1, i2);
     r1 += 2;
     writefln ("r1 += 2;   Ref: r1=%d r2=%d", r1, r2);
     writefln ("r1 += 2; InOut: i1=%d i2=%d", i1, i2);
     i2 += 2;
     writefln ("i1 += 2;   Ref: r1=%d r2=%d", r1, r2);
     writefln ("i1 += 2; InOut: i1=%d i2=%d", i1, i2);
 }

 !! That may cause interesting results !!

Maybe... assert(&r1 != &r2); assert(&r1 != &i1); assert(&r1 != &i2); assert(&r2 != &i1); assert(&r2 != &i2); assert(&i1 != &i2);

It's a pity that this doesn't work: assert(r1 is r2); ..etc.. Regan
Sep 14 2007
prev sibling parent reply Downs <default_357-line yahoo.de> writes:
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Regan Heath wrote:
 Wilhelm wrote:
 Who do I determine in a function (probably with a assert statement)
 that the function with more the one ref/inout parameters of the same
 type are not called with the same variable like:

 private import std.stdio;

 int main(char[][] args)
 {
     int i=3;
     test( i,i,i,i);
     return 0;
 }


 void test(ref int r1, ref int r2, inout int i1, inout int i2)
 {
     writefln ("           Ref: r1=%d r2=%d", r1, r2);
     writefln ("         InOut: i1=%d i2=%d", i1, i2);
     r1 += 2;
     writefln ("r1 += 2;   Ref: r1=%d r2=%d", r1, r2);
     writefln ("r1 += 2; InOut: i1=%d i2=%d", i1, i2);
     i2 += 2;
     writefln ("i1 += 2;   Ref: r1=%d r2=%d", r1, r2);
     writefln ("i1 += 2; InOut: i1=%d i2=%d", i1, i2);
 }

 !! That may cause interesting results !!

Maybe... assert(&r1 != &r2); assert(&r1 != &i1); assert(&r1 != &i2); assert(&r2 != &i1); assert(&r2 != &i2); assert(&i1 != &i2); Regan

void assertUnique(T)(ref T[] values...) { foreach (index, ref value; values[0..$-1]) foreach (ref value2; values[index+1..$]) assert(&value != &value2); } Untested, but should work. --downs -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.7 (GNU/Linux) Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org iD8DBQFG6oe3pEPJRr05fBERAviDAJ4ohNm2PvD1pWq9Z9AuZ5AdPqUT8wCeLkkW skn/AMq7xrLSUaN3vKy/R9c= =MfLi -----END PGP SIGNATURE-----
Sep 14 2007
parent reply "Jarrett Billingsley" <kb3ctd2 yahoo.com> writes:
"Downs" <default_357-line yahoo.de> wrote in message 
news:fce15c$1h7e$1 digitalmars.com...

 To clean it up a little

 void assertUnique(T)(ref T[] values...) {
  foreach (index, ref value; values[0..$-1])
    foreach (ref value2; values[index+1..$])
      assert(&value != &value2);
 }

Probably not _quite_ what you'd expect. "ref T[]" means "ref(T[])", not "(ref T)[]". That is, a reference to an array, not an array of references. So it'd have to be "T*[] values..." instead, and called as "assertUnique(&r1, &r2, &i1, &i2)".
Sep 14 2007
parent reply Downs <default_357-line yahoo.de> writes:
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Jarrett Billingsley wrote:
 "Downs" <default_357-line yahoo.de> wrote in message 
 news:fce15c$1h7e$1 digitalmars.com...
 
 To clean it up a little

 void assertUnique(T)(ref T[] values...) {
  foreach (index, ref value; values[0..$-1])
    foreach (ref value2; values[index+1..$])
      assert(&value != &value2);
 }

Probably not _quite_ what you'd expect. "ref T[]" means "ref(T[])", not "(ref T)[]". That is, a reference to an array, not an array of references. So it'd have to be "T*[] values..." instead, and called as "assertUnique(&r1, &r2, &i1, &i2)".

How about this? void assertUnique(T...)(ref T tuple) { foreach (index, ref v1; tuple[0..$-1]) foreach (ref v2; tuple[index+1..$]) assert(&v1 != &v2); } --downs -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.7 (GNU/Linux) Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org iD8DBQFG6vrqpEPJRr05fBERAsaGAKCYdvYlmyrI7ZnXend63q4YFMthwQCeLKNx wLtitWH8V2R4bCu5SBRTcZ0= =L2tS -----END PGP SIGNATURE-----
Sep 14 2007
parent "Jarrett Billingsley" <kb3ctd2 yahoo.com> writes:
"Downs" <default_357-line yahoo.de> wrote in message 
news:fcetv4$6ii$1 digitalmars.com...

 void assertUnique(T...)(ref T tuple) {
  foreach (index, ref v1; tuple[0..$-1])
    foreach (ref v2; tuple[index+1..$])
      assert(&v1 != &v2);
 }

Hm. It doesn't compile with the 'ref' for the foreach loop values, but when you take those out, it doesn't work. It works if you just index the tuple instead, though: void assertUnique(T...)(ref T tuple) { foreach(i1, v1; tuple[0 .. $ - 1]) foreach(i2, v2; tuple[i1 + 1 .. $]) assert(&tuple[i1] != &tuple[i1 + 1 + i2]); } Sadly, this can't be used in a function precondition because it gives the error "cannot modify parameter in precondition". The compiler thinks you're trying to modify the parameters when you pass them to assertUnique. (A case where "const ref" would be useful! How about that.) But you can still call it at the beginning of the function.
Sep 15 2007