Thread overview
identic ref/inout parameter
Sep 14, 2007
Wilhelm
Sep 14, 2007
Regan Heath
Sep 14, 2007
Regan Heath
Sep 14, 2007
Downs
Sep 14, 2007
Downs
September 14, 2007
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 !!


September 14, 2007
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
September 14, 2007
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
September 14, 2007
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
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);
}

Untested, but should work.
 --downs
September 14, 2007
"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)".


September 14, 2007
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)".
> 
> 
Damn, you're right.
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
September 15, 2007
"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.