Local Generic Constraints #7852
Answered
by
HaloFour
YT-GameMaker
asked this question in
Language Ideas
-
I would like something similar to this: void HandleReferenceType<T>(in T value) where T : class { }
void HandleValueType<T>(in T value) where T : struct { }
void HandleEnumType<T>(in T value) where T : unmanaged, Enum { }
void HandleCustomType<T>(in T value) where T : ICustom { }
[MethodImpl(MethodImplOptions.AggressiveInlining)]
void Handle<T>(in T value)
{
if (T : ICustom)
HandleCustomType(value);
else if (T : unmanaged, Enum)
HandleEnumType(value);
else if (T : struct)
HandleValueType(value);
else if (T : class)
HandleReferenceType(value);
else
throw new NotSupportedException();
} As shown in this example, local generic constraints are replaced with |
Beta Was this translation helpful? Give feedback.
Answered by
HaloFour
Jan 17, 2024
Replies: 2 comments 1 reply
-
Want it. I think void HandleReferenceType<T>(in T value) where T : class { }
void HandleValueType<T>(in T value) where T : struct { }
void HandleEnumType<T>(in T value) where T : unmanaged, Enum { }
void HandleCustomType<T>(in T value) where T : ICustom { }
[MethodImpl(MethodImplOptions.AggressiveInlining)]
void Handle<T>(in T value) where T : class or struct or (unmanaged, Enum) or ICustom
{
if (T is ICustom)
HandleCustomType(value);
else if (T is unmanaged and Enum)
HandleEnumType(value);
else if (T is struct)
HandleValueType(value);
else if (T is class)
HandleReferenceType(value);
else
throw new NotSupportedException();
} |
Beta Was this translation helpful? Give feedback.
1 reply
-
See: #6308 |
Beta Was this translation helpful? Give feedback.
0 replies
Answer selected by
YT-GameMaker
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
See: #6308