Collection initialization syntax with out/ref parameters #3015
Replies: 3 comments
-
Given how niche this would be (effectively just your api) it would be making an API to support far too narrow a case. |
Beta Was this translation helpful? Give feedback.
-
Even if you were to spec and implement this yourself, and it required no review, and you wrote the documentation, etc. the complexity of the implementation would have to live on in the compiler forever. When you compare that to simply writing your add manually, the decision seems obvious. However IMO there is a reason, albeit not a strong one. If an Add method returns an out parameter, then it's not strictly an initialisation method. It does more, and so doesn't belong in an object initialiser. |
Beta Was this translation helpful? Give feedback.
-
Sorry for commenting on the wrong issue. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
I have an existing library that uses the convention-based collection initialization syntax to cleanly handle a mixture of
Add
methods. For a new requirement, I built anAdd
method that requires anout
parameter. When trying to use this method within the list init syntax, I discovered that this is not supported. This struck me as odd/unintuitive given that the syntax is sugar around direct method calls.Contrived example:
Are there specific reasons not to support out/ref paramters in collection initialization methods (other than effort/value/prioritization)? Would this be an acceptible addition to the language?
Beta Was this translation helpful? Give feedback.
All reactions