C
C#2y ago
Hugh

❔ Generics with different input and output type

I'm writing a generic at the moment which it something like this:
T GetSomething<T, U>(U InputValues)
T GetSomething<T, U>(U InputValues)
When I'm calling it, is there any way to only specify T without specifying U (as this can be inferred from the argument being passed in). Ideally I'd like to be able to type something like:
MyInputType inputValues;
MyType result = GetSomething<MyType>(inputValues);
MyInputType inputValues;
MyType result = GetSomething<MyType>(inputValues);
rather than
MyInputType inputValues;
MyType result = GetSomething<MyType, MyInputType>(inputValues);
MyInputType inputValues;
MyType result = GetSomething<MyType, MyInputType>(inputValues);
Is something like this possible at all?
6 Replies
ero
ero2y ago
no
Hugh
HughOP2y ago
ok
ero
ero2y ago
ask #roslyn if you really wanna know why
Hugh
HughOP2y ago
I get that it isn't something you can do in other languages - I was just wondering if it were different here 🙂
333fred
333fred2y ago
You can do it in other languages using associated types. Maybe we'll get them someday
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server