Syntax for specifying User-defined conversion operators #7903
Replies: 3 comments 1 reply
-
The most common answer to all questions of this nature is simply "It hasn't been needed yet". Features get added when there's a well-defined need, when a good enough solution is found (noting that no solution ever satisfies everyone), and when the effort of implementation, support, and documentation justifies the investment. Twenty years ago (wow!), Eric Gunnerson was on the C# team and he summarized the issues well in his famous (and oft cited) post -100 points. In this specific case, the most likely reason there's no syntax is that it's trivial to rewrite the code to make the problem go away. Adding specific syntax to handle every possible scenario would quickly give us a labyrinthian and obtuse language that defied motral understanding. |
Beta Was this translation helpful? Give feedback.
-
For this ambiguity to happen, the two types have to be in the same compilation unit to reference each other. The author of it can change the two operators to only one. |
Beta Was this translation helpful? Give feedback.
-
Maybe the new overload resolution attribute could be used for this? |
Beta Was this translation helpful? Give feedback.
-
Consider two classes A and B:
This causes Compiler Error CS0457 Ambiguous user defined conversions. To my best knowledge (correct me if I am wrong), there is no way to specify which conversion I want to use in this specific scenario. If that is the case, could syntax for specifying these unary conversion operators be added to the language? E.g.
B.B(a)
or(B.B)a
or some other. Not that I am necessarily asking to add it, I don't think it's needed by many people, I am mostly just curious if it is possible to be added and why isn't it there - is it by design/is it an oversight/is it unimportant etc.Beta Was this translation helpful? Give feedback.
All reactions