www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - in vs inout

reply Arredondo <arm.plus gmail.com> writes:
The recent change log for v2.092.0 Beta says that with the new 
implementation for the `in` storage class:

 `in` should be the storage class of choice for purely input 
 function parameters.
I had been using inout for some time now for "purely input function parameters". Is there a case to be made in favor of const? why would be scope const preferred over scope inout? Cheers! Arredondo.
Apr 30 2020
parent reply Adam D. Ruppe <destructionator gmail.com> writes:
On Thursday, 30 April 2020 at 14:00:40 UTC, Arredondo wrote:
 I had been using inout for some time now for "purely input 
 function parameters".
`inout` is more specifically for things you take in, look at, then pass back out. So it forms part of your return value. `const` is for when you are just looking without returning any of it. scope inout may be more appropriate if you are going to return it at all...
Apr 30 2020
parent Steven Schveighoffer <schveiguy gmail.com> writes:
On 4/30/20 10:52 AM, Adam D. Ruppe wrote:
 On Thursday, 30 April 2020 at 14:00:40 UTC, Arredondo wrote:
 I had been using inout for some time now for "purely input function 
 parameters".
`inout` is more specifically for things you take in, look at, then pass back out. So it forms part of your return value.
We have discovered actually that inout has some nice properties even if you aren't returning it. For example: void rearrange(inout(int)*[] arr); This can actually accept const(int)*[], int*[], and immutable(int)*[] with one function. The function can edit the array without editing any of the values pointed at. But in terms of 'in' vs. 'inout', I'd say 'in' unless you have a specific reason to use inout (the above included). -Steve
Apr 30 2020