www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - inout based on this reference?

reply "Simen kjaeraas" <simen.kjaras gmail.com> writes:
Is it possible to make a function return dependent upon the constancy of  
the this reference?

i.e. this:

struct foo {
    int n;
    inout( ref int ) getn( ) inout {
        return n;
    }
}


-- 
Simen
Aug 29 2010
parent reply "Steven Schveighoffer" <schveiguy yahoo.com> writes:
On Sun, 29 Aug 2010 17:36:24 -0400, Simen kjaeraas  
<simen.kjaras gmail.com> wrote:

 Is it possible to make a function return dependent upon the constancy of  
 the this reference?

 i.e. this:

 struct foo {
     int n;
     inout( ref int ) getn( ) inout {
         return n;
     }
 }
Yes. That is the bread-and-butter usage for inout. Although inout is currently horribly broken. -Steve
Aug 30 2010
next sibling parent "Steven Schveighoffer" <schveiguy yahoo.com> writes:
On Mon, 30 Aug 2010 09:08:11 -0400, Steven Schveighoffer  
<schveiguy yahoo.com> wrote:

 On Sun, 29 Aug 2010 17:36:24 -0400, Simen kjaeraas  
 <simen.kjaras gmail.com> wrote:

 Is it possible to make a function return dependent upon the constancy  
 of the this reference?

 i.e. this:

 struct foo {
     int n;
     inout( ref int ) getn( ) inout {
         return n;
     }
 }
Yes. That is the bread-and-butter usage for inout. Although inout is currently horribly broken.
I should point out, the function sig should be: ref inout(int) getn() inout inout is a type modifier, ref is a storage class. -Steve
Aug 30 2010
prev sibling parent reply "Simen kjaeraas" <simen.kjaras gmail.com> writes:
Steven Schveighoffer <schveiguy yahoo.com> wrote:
0123456789012345678901234567890123456789012345678901234567890123456789012
 On Sun, 29 Aug 2010 17:36:24 -0400, Simen kjaeraas  
 <simen.kjaras gmail.com> wrote:

 Is it possible to make a function return dependent upon the constancy  
 of the this reference?

 i.e. this:

 struct foo {
     int n;
     inout( ref int ) getn( ) inout {
         return n;
     }
 }
Yes. That is the bread-and-butter usage for inout. Although inout is currently horribly broken.
So horribly broken that example does not work at all, or is that just me? This is my code, verbatim: ////////////////////////// module foo; struct bar { int n; ref inout( int ) getN( ) inout { return n; } } void main( ) { } ////////////////////////// And it gives this message: foo.d(5): Error: inout on return means inout must be on a parameter as well for inout inout(int)() -- Simen
Aug 30 2010
parent "Steven Schveighoffer" <schveiguy yahoo.com> writes:
On Mon, 30 Aug 2010 13:49:20 -0400, Simen kjaeraas  
<simen.kjaras gmail.com> wrote:

 Steven Schveighoffer <schveiguy yahoo.com> wrote:
 0123456789012345678901234567890123456789012345678901234567890123456789012
 On Sun, 29 Aug 2010 17:36:24 -0400, Simen kjaeraas  
 <simen.kjaras gmail.com> wrote:

 Is it possible to make a function return dependent upon the constancy  
 of the this reference?

 i.e. this:

 struct foo {
     int n;
     inout( ref int ) getn( ) inout {
         return n;
     }
 }
Yes. That is the bread-and-butter usage for inout. Although inout is currently horribly broken.
So horribly broken that example does not work at all, or is that just me? This is my code, verbatim: ////////////////////////// module foo; struct bar { int n; ref inout( int ) getN( ) inout { return n; } } void main( ) { } ////////////////////////// And it gives this message: foo.d(5): Error: inout on return means inout must be on a parameter as well for inout inout(int)()
Yes, horribly broken means does not work in almost all cases :) You should defer using inout until it is fixed. As of now, it's unusable (Walter is aware and has it on his TODO list). Please vote for bug http://d.puremagic.com/issues/show_bug.cgi?id=3748 -Steve
Aug 30 2010