Proposal: Function Parameter Type Converters #2230
Replies: 5 comments
-
This does sound like a strong case for discriminated unions, doesn't it? |
Beta Was this translation helpful? Give feedback.
-
@Unknown6656 - |
Beta Was this translation helpful? Give feedback.
-
Overloading is already one of the most complicated aspects of the C# language, having some arbitrary converter in front of that which can convert between arbitrary types seems like it would explode into a Cartesian product of problems. |
Beta Was this translation helpful? Give feedback.
-
I may be mistaken, but you would only have to write one extension method addressing the union. In this method you would write your converter logic, e.g.: // this is just an example syntax ..... you get the idea ^^
public class MyUnion = long A | long[] B | Portal C | Portal[] D;
public static class MyExtensions
{
public static T ConvertTo<T>(this MyUnion obj) =>
obj switch {
// switch on the union and return the converted values
MyUnion.A a => ...
MyUnion.B b => ...
MyUnion.C c => ...
MyUnion.D d => ...
}
public static void MyMethod()
{
MyUnion u = new[] { 42L, -4L, 77L };
int i = u.ConvertTo<int>();
}
} |
Beta Was this translation helpful? Give feedback.
-
Hi All, What are your thoughts around this code?
|
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Function Parameter Type Converters
Summary
This proposal provides a compiler construct that eliminates the need to generate multiple method overloads for similar objects. This will eliminate boilerplate code and simplify method design.
Motivation
Often when writing APIs, developers are forced to provide multiple overloads to a function to accommodate different-yet-similar parameter types. For example, a class may have multiple overloads of a "DeleteUser" method as follows:
In the design above, adjusting the method to allow single or multiple
P
's that are eitherPortal
's orlong
's would result in NxM additional overloads being necessary (in this case, 16 different variants).Detailed Design
Introduce
ParameterConverterAttribute<T>
andIParameterConverter<TIn, TOut>
ParameterConverterAttribute is a generic attribute that takes the type of an object that implements one or more IParameterConverter interfaces.
The implementation below would accommodate all 16 variants of the above method:
Editor Experience
When programming through Visual Studio, automatic parameter conversions could be displayed as follows:

This makes it easy for the developer to understand that some magic is happening while still making it clear what is happening. This is somewhat similar to how extension methods surface.
Considerations and Objections
Call-Site Conversions and Extension Methods
It is possible to design extension methods that would handle the conversion, however, extension methods:
Type Converters
Someone might propose adding type converters to the class:
However, this is only possible if you can edit the source code of the class itself.
Discriminated Unions
Although discriminated unions are not yet part of C#, this approach would somewhat simplify the challenge, although not fully, because it would force the function writer to deal with the different types inside of the function itself. This is arguably worse than exposing multiple overloads because of the extra cases it introduces in the method body.
For example:
Static Methods
Instead of using an instance class, it may be possible to look up a static member on the type provided to the ParameterConverter. I'm unsure of the implementation details, but this would be a good idea.
Beta Was this translation helpful? Give feedback.
All reactions