You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
PublicStructureSomeStructPublicFieldAAsStringEndStructureModuleModule1SubMain()DimmyArray(0)AsSomeStructWithmyArray(0).FieldA="New FieldA value"EndWith'Outputs: FieldA was changed to New FieldA value Console.WriteLine($"FieldA was changed to {myArray(0).FieldA}")Console.ReadLineEndSubEndModule
Erroneous output
publicpartialstructSomeStruct{publicstringFieldA;}internalstaticpartialclassModule1{publicstaticvoidMain(){varmyArray=newSomeStruct[1];{varwithBlock=myArray[0];withBlock.FieldA="New FieldA value";}// Outputs: FieldA was changed toConsole.WriteLine($"FieldA was changed to {myArray[0].FieldA}");Console.ReadLine();}}
Expected output
Not sure what the best solution is here.
I was about to suggest copying the withBlock variable back to the original element at the end.
But if the structure has methods that are called somewhere halfway the With block, then that method would be operating on a copy of the structure so the converted code would still behave differently from the original.
Alternatively, the With block could be converted to a local method, with the original element passed by reference, but I was struggling to get the syntax right for that. Maybe I was trying something that C# doesn't support (or I just was too much in a hurry).
Details
Simple object expressions work fine, because in those cases the converter does not introduce a withBlock variable, and assignments within the block keep operating directly on the 'subject' of the original With block.
The issue is also occurring for structs that are members of a class (With someObject.SomeStructure).
I didn't try with other value types but maybe the same issue occurs there too.
Thanks for this. Mutating structs is a subtle area!
I'm a bit busy at the moment so it might be a while before I have space to dig into this one. I'm sure there must be a way though ☺
Input code
Erroneous output
Expected output
Not sure what the best solution is here.
I was about to suggest copying the withBlock variable back to the original element at the end.
But if the structure has methods that are called somewhere halfway the With block, then that method would be operating on a copy of the structure so the converted code would still behave differently from the original.
Alternatively, the With block could be converted to a local method, with the original element passed by reference, but I was struggling to get the syntax right for that. Maybe I was trying something that C# doesn't support (or I just was too much in a hurry).
Details
The text was updated successfully, but these errors were encountered: