Nested collection initializer doesn't capture the collection in a temp #2066
Unanswered
gafter
asked this question in
Language Ideas
Replies: 1 comment
-
While I believe the default behavior could be a lowering strategy only on certain circumstances, changing it in any ways is likely considered as a breaking change because side effects will be partially lost. Not sure if that's debatable or worth taking. |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
@alrz commented on Mon Dec 10 2018
Version Used: 7.3
Steps to Reproduce:
Expected Behavior:
Actual Behavior:
This was surprising to me, because
P
is appeared once in the source, so I'd expect a single call to it.@CyrusNajmabadi commented on Mon Dec 10 2018
Spec says:
This is unfortunately pretty vague. However, the spec also happens to give some examples that shows that the behavior of the compiler is working as expected. i.e.:
So i'd say the compiler is working exactly as expected given that this is exactly how the spec says it should translate.
@alrz commented on Mon Dec 10 2018
Thanks for the pointer.
I'm wondering what's the rationale behind this, since we already ruled out value types:
@CyrusNajmabadi commented on Mon Dec 10 2018
Note: this is likely necessary for the case of initializing a mutable struct with fields. If a temp was made, then nested assignments wouldn't actually update the actual struct in-situ.
@CyrusNajmabadi commented on Mon Dec 10 2018
Because the property would return a copy, hence, any assignments/mutations would be effectively lost in nearly all cases.
@CyrusNajmabadi commented on Mon Dec 10 2018
@alrz Imagine if we had:
The compiler translates this to:
This is exactly what we want here, and completely initializes the struct with no unnecessary copies. If we instead had:
This wouldn't even be correct. We'd have to copy the values back with something like:
And this presumes that the location is writable (which it might not be for a readonly-field or prop). So, the simplest approach, which works in all cases, is to simply write things out like how the compiler does things. The only 'bad' side is the potential for a slight amount of extra computation on properties to retrieve a value that will probably be the same value. But trying to optimize that case out somehow likely has never been worth it.
@alrz commented on Mon Dec 10 2018
Yeah, so this was only necessary for structs, but for consistent side-effects we still do this for reference types. Unfortunate.
@alrz commented on Mon Dec 10 2018
You're using fields (which have no side-effects), but with properties this still produce the call for each value, in which case we get a copy anyways? So it looks like a special lowering for structs (struct+field) has been done for anything else!
@CyrusNajmabadi commented on Mon Dec 10 2018
With properties, this would not be allowed, as these are value types.
@gafter commented on Tue Dec 11 2018
Since we appear to be acting as expected, moving to
csharplang
to reconsider how we want it to behave.Beta Was this translation helpful? Give feedback.
All reactions