Thread overview
Meaning of in, out and inout
Jan 20, 2022
Sergey
Jan 20, 2022
Paul Backus
Jan 20, 2022
Sergey
January 20, 2022
https://forum.dlang.org/post/17nwtnp4are5q$.1ddtvmj4e23iy.dlg@40tude.net

On Tuesday, 10 May 2005 at 01:06:14 UTC, Derek Parnell wrote:
> On Tue, 10 May 2005 00:30:57 +0000 (UTC), Oliver wrote:
>
>> Hello D-ers
>> 
>> The documentation is very short on the keywords in, out and inout.
>> Is is inout sth like a reference ? But then, what is in and what is out?
>
>
> in:
> The argument is preserved, such that when control returns to the caller,
> the argument as passed by the caller is unchanged. This means that the
> called function can do anything it likes to the argument but those changes
> are never returned back to the caller. There is a bit of confusion here
> when it comes to passing class objects and dynamic arrays. In both these
> cases, a reference to the data is passed. Which means that for 'in'
> references, the called function is free to modify the reference data (which
> is what is actually passed) in the full knowledge that any changes will
> *not* be returned to the caller. However, if you make any changes to the
> data being referenced, that modified data is 'returned'. Which means that,
> for example, if you pass a char[] variable, the reference will be preserved
> but the data in the string can be changed.
>
> out:
> The argument is always initialized automatically by the called function
> before its code is executed. Any changes to the argument by the called
> function are returned to the caller. The called function never gets to see
> the value of the argument as it was before the called function gets
> control. The argument must a RAM item and not a literal or temporary value.
>
> inout:
> The argument is passed to the called function without before its code is
> executed. Any changes to the argument by the called function are returned
> to the caller. In other words, the called function can see what value was
> passed to it before changing it.  The argument must a RAM item and not a
> literal or temporary value.
>
>
> Examples:
>
>   char[] a;
>   int    b;
>
>   void func_one(in char[] X, in int Y)
>   {
>       X[0] = 'a';   // Modifies the string contents.
>       X = "zxcvb";  // Modifies the string reference but is not returned.
>
>       Y = 3; // Modifies the data but is not returned.
>   }
>
>
>   a = "qwerty";
>   b = 1;
>   func_one(a,b);
>
>   writefln("%s %d", a,b); // --> awerty 1
>
>   void func_two(out char[] X, out int Y)
>   {
>       X[0] = 'a';   // Modifies the string contents.
>       X = "zxcvb";  // Modifies the string reference.
>       if (b == 1)
>         Y = 3; // never executed because Y is always zero on entry.
>       else
>         Y = 4; // Modifies the data.
>   }
>
>
>   a = "qwerty";
>   b = 1;
>   func_two(a,b);
>
>   writefln("%s %d", a,b); // --> zxcvb 4
>
>   void func_three(inout char[] X, inout int Y)
>   {
>       X[0] = 'a';   // Modifies the string contents.
>       X = "zxcvb";  // Modifies the string reference.
>       if (b == 1)
>         Y = 3; // Modifies the data.
>       else
>         Y = 4; // Modifies the data.
>   }
>
>
>   a = "qwerty";
>   b = 1;
>   func_two(a,b);
>
>   writefln("%s %d", a,b); // --> zxcvb 3

Thanks a lot for your explanation.
I started to learn D language recently and I have trouble with understanding some part of language.
I do your example on Linux and it works very well, especially func_rthee().
when I try to repeat code on Windows 10, I get errors:
   Error: cannot modify `inout` expression `X[0]`
   Error: cannot modify `inout` expression `X`
   Error: cannot modify `inout` expression `Y`
   Error: cannot modify `inout` expression `Y`
Very strange situation for me.
I expect next behavior of the parameters X and Y could get value and could change it on Windows but now I am not sure what is goin on.

PS. I am using DMD64 D Compiler v2.098.1-dirty on Debian 10 Linux and Windows 10.
January 20, 2022
On Thursday, 20 January 2022 at 13:19:06 UTC, Sergey wrote:
> https://forum.dlang.org/post/17nwtnp4are5q$.1ddtvmj4e23iy.dlg@40tude.net
>
> On Tuesday, 10 May 2005 at 01:06:14 UTC, Derek Parnell wrote:
>> [...]
>
> Thanks a lot for your explanation.
> I started to learn D language recently and I have trouble with understanding some part of language.
> I do your example on Linux and it works very well, especially func_rthee().
> when I try to repeat code on Windows 10, I get errors:
>    Error: cannot modify `inout` expression `X[0]`
>    Error: cannot modify `inout` expression `X`
>    Error: cannot modify `inout` expression `Y`
>    Error: cannot modify `inout` expression `Y`
> Very strange situation for me.
> I expect next behavior of the parameters X and Y could get value and could change it on Windows but now I am not sure what is goin on.
>
> PS. I am using DMD64 D Compiler v2.098.1-dirty on Debian 10 Linux and Windows 10.

The explanation you quoted is from 2005, and `inout` does not mean the same thing in 2022 as it did in 2005.

The current meaning of inout is explained in the D language specification on dlang.org. Here is a link to the relevant section:

https://dlang.org/spec/function.html#inout-functions
January 20, 2022
On Thursday, 20 January 2022 at 13:28:54 UTC, Paul Backus wrote:
> On Thursday, 20 January 2022 at 13:19:06 UTC, Sergey wrote:
>> [...]
>
> The explanation you quoted is from 2005, and `inout` does not mean the same thing in 2022 as it did in 2005.
>
> The current meaning of inout is explained in the D language specification on dlang.org. Here is a link to the relevant section:
>
> https://dlang.org/spec/function.html#inout-functions

Thanks a lot.
January 20, 2022

On 1/20/22 8:28 AM, Paul Backus wrote:

>

The explanation you quoted is from 2005, and inout does not mean the same thing in 2022 as it did in 2005.

The current meaning of inout is explained in the D language specification on dlang.org. Here is a link to the relevant section:

https://dlang.org/spec/function.html#inout-functions

Note that in 2005, D2 did not exist, inout in D1 used to mean what ref now means in D2.

So if you are looking for that specific meaning, use ref.

-Steve