Problems with lambdas that capture multiple variables (HAST-286)
sarahelsaig opened this issue · comments
When you create a non-static lambda function that captures more than two variables, the decompiler creates seemingly invalid syntax that probably wouldn't compile as C# and the transformed VHDL doesn't compile in v++. This may be an ICSharpCode.Decompiler bug, we can investigate this possibility after we've upgraded to the latest version (see #84).
See an example of the problem here. Originally I tried
tasks[index] = Task.Factory.StartNew(index => ((int)index + step) * heightFactor, index);
but then the decompiled & processed syntax tree would generate display classes with only one field each:
[CompilerGenerated]
private sealed class <>c__DisplayClass7_0
{
public int heightFactor;
}
[CompilerGenerated]
private sealed class <>c__DisplayClass7_1
{
public int step;
public <>c__DisplayClass7_0 CS$<>8__locals1;
[NullableContext (2)]
internal int <CreateMatrix>b__0 (int index)
{
return (index + this.step) * this.CS$<>8__locals1.heightFactor;
}
}
Note that this.CS$<>8__locals1.heightFactor;
is invalid according to VS Code, Rider and GitHub syntax highlighting. This is the build output log showing the errors according to v++. Besides that, it's also unnecessarily complex to have two nested display classes instead of just one for such a simple mathematical expression. Manually creating the class that contains all values so nothing is captured by the lambda (as I did here) is a valid workaround. In this case the lambda is effectively static so it results in no compiler generated classes.