Add ability to provide array type conversion for a type #1546
-
If I create a class and give it an implicit conversion operator (in C#):
It'd be nice to also be able to provide an array converter as well, e.g.
Currently, the implicit conversion won't happen automatically for the array (even with the singular implicit converter defined); so making an extension to the other type to provide a helper seems to be the only route but a bit messier. Also found similar questions for this process dating back to 2010 here. It also seems like there should be something similar for VB? https://stackoverflow.com/questions/1312549/is-there-a-way-to-define-an-implicit-conversion-operator-in-vb-net Originally filed here |
Beta Was this translation helpful? Give feedback.
Replies: 4 comments 2 replies
-
What makes arrays special? Wouldn't you also want to be to convert e.g. |
Beta Was this translation helpful? Give feedback.
-
I just faced the same problem a few minutes ago! |
Beta Was this translation helpful? Give feedback.
-
I would very much not like an implicit operator making a full copy of an array behind the scenes. |
Beta Was this translation helpful? Give feedback.
-
C# and .net already have semantics around array conversions. They do not involve new arrays being allocated, and introducing such a feature would be a massive footgun.
There is already Both of these are good because it is clear from the code that you are doing some sort of implicit conversion, but are actually performing computation that could be expensive and which can produce a new collection instance. There is no need (or desire) for a language feature here when the code you can write is trivial and already provided by the framework. And that's doubly true given the huge potential problems that could come with this feature.
I disagree that it's message. It's a nice bit of explicit code that is easy to understand, idiomatic, and plainly calls out the cost and semantics of what are happening. That's absolutely what you want when operating on arbitrary sized pieces of data. :) |
Beta Was this translation helpful? Give feedback.
I would very much not like an implicit operator making a full copy of an array behind the scenes.