So what does (inout int = 0) do?

Timon Gehr via Digitalmars-d digitalmars-d at puremagic.com
Fri Apr 15 12:48:38 PDT 2016


On 15.04.2016 17:22, Steven Schveighoffer wrote:
> On 4/14/16 11:10 PM, Andrei Alexandrescu wrote:
>> Consider:
>>
>> https://github.com/D-Programming-Language/phobos/blob/master/std/range/primitives.d#L152
>>
>
> It works around a limitation of inout that isn't necessary (even though
> I thought it was being helpful when I suggested it). That is, functions
> without inout parameters cannot declare local inout variables. But this
> isn't really necessary, and should be fixed. I will discuss this in my
> talk in a few weeks.
> ...

That's potentially dangerous. What about cases like the following?

void main(){
     inout(int)[] x=[1,2,3];
     immutable(int) a;
     int b;
     inout(int)[] foo(inout int){
         return x;
     }
     immutable(int)[] y=foo(a);
     int[] z=foo(b);
}



> Note, the =0 part isn't necessary right now, since it's not called. It's
> just used to test if the function can compile.
>
> In short, my opinion on inout is that it has some unnecessary
> limitations, which can be removed, and inout will work as mostly
> expected. These requirements to work around the limitations will go away.
> ...

Other important limitations of inout are e.g.:
- inout variables cannot be fields.
- There can be only one inout in scope.


> I expect after reading this thread that the Q&A will be.. interesting.
>
> -Steve




More information about the Digitalmars-d mailing list