Using inout in delegate
Steven Schveighoffer
schveiguy at yahoo.com
Thu Mar 28 07:16:29 PDT 2013
On Thu, 28 Mar 2013 04:34:36 -0400, Jacob Carlborg <doob at me.com> wrote:
> The following code fails to compile:
>
> void foo (inout int[] arr)
> {
> auto dg = {
> foreach (i, e ; arr) {}
> };
> dg();
> }
>
> void main ()
> {
> auto a = [3, 4, 5];
> foo(a);
> }
>
> Error message:
>
> main.d(9): Error: variable main.foo.__lambda1.__aggr1174 inout variables
> can only be declared inside inout functions
> main.d(9): Error: variable main.foo.__lambda1.e inout variables can only
> be declared inside inout functions
>
> If I remove the delegate everything compiles. Am I doing something wrong?
>
Like Timon said, it's a bug in inout design.
I'm not sure what __aggr1174 is, but you can fix the e error by specifying
the type for e (or specifying it as const).
I'm assuming the issue is that the compiler is trying to generate a struct
to hold the stack data for foo, and struct members cannot be inout.
It is a difficult problem to solve, because inout has two meanings
depending on whether it is a parameter/return or a local variable. At
some point, we need to address this, because inout has so much potential,
but suffers from some large deficiencies.
-Steve
More information about the Digitalmars-d-learn
mailing list