Allow discard ref and in params by boxing their values directly #1911
Replies: 14 comments
-
This seems like this would happen so rarely that it really wouldn't benefit from having a language feature to improve thigns here. How often are you encountering this? |
Beta Was this translation helpful? Give feedback.
-
@CyrusNajmabadi |
Beta Was this translation helpful? Give feedback.
-
This used to be a painfully common occurrence when working with COM interop where so often all of the parameters would be |
Beta Was this translation helpful? Give feedback.
-
I thought C# made it so you could jsut do the com interop without providing those optional ref parameters (or without requiring you to use 'ref'). Is this a real problem anymore? |
Beta Was this translation helpful? Give feedback.
-
Er... It's hard to justify a language feature when there's no actual real-world examples demonstrating the value :) |
Beta Was this translation helpful? Give feedback.
-
Does that apply to all It's been a while since I've had to do COM interop with C#, largely against our own VB6 libraries but with Office and third-party COM libraries thrown in for good measure. The Office interop was particularly painful since a lot of those methods accept a bazillion optional parameters but I recall it being obnoxious with the other libraries as well since you needed to separately declare and initialize variables just to satisfy the fact that the parameters were |
Beta Was this translation helpful? Give feedback.
-
So, a ton of hat original interop work was done based on feedback from many devs as well as the teams owning those old APIs. They were really happy with the results we got, and since then i can't even recall the last time anyone asked about more improvements here. That makes me take the default position that this isn't actually an issue people are encountering significantly enogh to warrant attention. Given that we don't even have an exemplar to work with here makes me very skeptical that there is sufficient value. |
Beta Was this translation helpful? Give feedback.
-
Are there any actual disadvantages to the feature though? I can't think of any. This particular feature doesn't affect me at all so I wouldn't implement it myself, but with other features which don't appear to have disadvantages, but aren't important enough to be championed, what's the policy if someone from the community offers to implement them? |
Beta Was this translation helpful? Give feedback.
-
There's no policy at all. If you implement them, there's no harm in that. But without a champion, there's no path forward from that implementation to making it into the language.
Sure. It has to be designed, spec'ed, implemented, documents, tested, made to work in all IDE scenarios, etc. etc. etc. There is a high cost to any language feature. As an example, look at the effort necessary to just support |
Beta Was this translation helpful? Give feedback.
-
This is already a feature of |
Beta Was this translation helpful? Give feedback.
-
You guys scratched the itch where Office is concerned, specifically APIs where the methods contain a metric ton of optional parameters. But it didn't help in the situation of APIs where the parameters are required. Those parameters still must be passed by To put this into perspective, it's not that uncommon for us to have methods that require upwards of 20 parameters. To call such a method from VB6 is a one-liner. To call such a method from VB.NET is a one-liner. To call such a method from C# requires 21 separate statements. This may be a relatively rare subset of the overall customers of C#, and supporting COM is probably in the same dustbin as .NET Framework, but it's certainly still painful for developers who have to straddle the worlds of Microsoft Technologies Past. Furthermore, and this is a bit of a tangent, there is some bizarre stuff in the C# language to enable COM interop that is not a part of the spec and, to my knowledge, can't be exploited by normal C# code. This includes optional |
Beta Was this translation helpful? Give feedback.
-
Just to note, I'm not expecting any movement on an issue like this, particularly if the reason is to improve the situation between VB6 and C#. I think that allowing |
Beta Was this translation helpful? Give feedback.
-
I thought we made it so that you didn't need 'ref' to pass these variables to com interfaces. Since hte vast majority were not actuall in/out params, we just allowed you to pass naked values, and made it work. |
Beta Was this translation helpful? Give feedback.
-
Maybe. I don't have any of the relevant COM interfaces around to check and I don't know what standard COM libraries that ship with Windows 10 might behave in a similar manner. If that's the case then the language already supports this behavior albeit in a limited way and not as part of the specification. I'd lump it with the other special COM language behaviors I mentioned above as something worth discussion. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
If I have this:
I suggest to allow discard any change to the ref parameter by boxing it's value directly like this:
Foo(1, ref "test");
And the same can be done for in params.
This will spare us from defining unnecessary variable that we don't need to reuse after calling the function. VB.NET already allows sending values directly to ref params, and since C# recently allowed discard out params, I think discarding ref and in params will be consistent with that.
Beta Was this translation helpful? Give feedback.
All reactions