Proposal: Fix generic methods when using out parameters #8730
Unanswered
ghost
asked this question in
Language Ideas
Replies: 1 comment
-
Generics don't work like templates. They don't specialize at compile time and they cannot take overloads into consideration. You'd need a generic single-item method which would internally specialize based on the generic type argument: public bool Read<T>(out T b)
{
if (typeof(T) == typeof(int)) {
bool result = Read(out int temp);
b = (T)(object)temp;
return result;
}
b = default(T);
return false;
} Generates some pretty awful IL but IIRC the JIT will produce optimized versions for value types. |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
I had to use code generation for one of my project to be able to serialize types over the network in a particular situation. Eventually some of these types had to be sometimes sent as single element, and sometimes in the form of multiple element. I for that added a generic method
I ended up adding up a special pass to my code generator to generate serialization extensions in the special use of List per type, which creates the unnecessary need of having to perform one more code generation if you suddenly need to serialize a list of a certain type. (and would create unnecessary code if it was generated for all types by default even when it's not needed)
Beta Was this translation helpful? Give feedback.
All reactions