dotnet / dotnet/vblang

Behaviour of With statement and Lambdas

Open
#72 0 comments 0 reactions 1 assignee Claimed by @AnthonyDGreen View on GitHub
Dominant language
No language data
Stars
328
Forks
71
PR merge metrics
No merged PRs in 30d

Description

I have found that the following code:

`With SomeObj
DoSomething(Function() .SomeField = SomeValue)
End With

DoSomething(Function() SomeObj.SomeField = SomeValue)`

...produces different results when the function DoSomething() updates the value of SomeObj before invoking the function passed in as a lambda.

The two calls to DoSomething() should behave identically, indeed when you do the "in-line with statement" refactoring from tools like CodeRush, it will transform the call inside the With statement into the call outside the With statement.

But they don't behave identically. It's almost like the variables used to capture SomeObj are different (decompiling the code with Reflector confirms this: a new closure object is introduced by the With statement, essentially "disconnecting" the object you think you are looking at based on the syntax from the run-time object actually used when the code is running).

So two questions:

1) is this "by design" and currently explained by the language spec? If so, where so I can read up on it?

2) if it is not by design, can the language/compiler be improved to either:
a) warn the user of potentially unexpected behaviour when lambdas reference members of objects accessed via the With statement.
b) produce the expected behaviour (With statement should not introduce a new closure object if the variable used is "already previously captured").

Hope this makes sense, I was unable to extract the code that produced unexpected behaviour, but if required, I can set up an isolated test case.

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.