Replies: 1 comment
-
I am not sure if i understand the problem, but this at SharpLab ... using System;
using static System.Console;
public static class Program {
public static void Main () {
AD ("foo");
AD (42);
AD (0xDeadBeef);
AD (3.14);
AD (3.14f);
AD (3.14m);
AD (NEClass.Default);
AD (NEStruct.Default);
WriteLine ();
AE ("foo");
AE (42);
AE (0xDeadBeef);
AE (3.14);
AE (3.14f);
AE (3.14m);
AE (NEClass.Default);
AE (NEStruct.Default);
}
public static void AD<T> (T arg) {
if (arg is IEquatable<T> arg_equatable)
D (arg_equatable);
else C (arg);
}
public static void AE<T> (T arg) {
// same as AD, but passing both the constrained AND unconstrained arg
if (arg is IEquatable<T> arg_equatable)
E (arg, arg_equatable);
else C (arg);
}
public static void B<T> (T arg) where T: IEquatable<T>
=> WriteLine ($"B: {typeof (T).Name}: {arg}");
public static void C<T> (T arg)
=> WriteLine ($"C: {typeof (T).Name}: {arg}");
public static void D<T> (IEquatable<T> arg)
=> WriteLine ($"D: {typeof (T).Name}: {arg}");
public static void E<T> (T t, IEquatable<T> te)
=> WriteLine ($"E: {typeof (T).Name} = {t.GetType ().Name} = {te.GetType ().Name} / {t} = {te}");
} // Program
public class NEClass { // not equatable class
public static NEClass Default => new ();
public override string ToString () => "'not equatable class instance'";
}
public readonly struct NEStruct { // not equatable struct
public static NEStruct Default => default;
public override string ToString () => "'not equatable struct instance'";
} .. will print:
|
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.
-
The problem:
Do we have any C# language constructs that allows to call generic method with more restrictions on type than caller method, if we have proofs it's safe (Higher Kinder Polymorphism, advanced pattern matching, etc) ?
Do we have any option to make a weird unsafe hack to make unallowed generic method call ?
Looks like we can express the call on IL level (Fody + https://github.com/ltrzesniewski/InlineIL.Fody) or somehow Function Pointers can be used.
(Sure reflection can help, but it's expansive and might cause an issues with the CoreRT native compilation).
Beta Was this translation helpful? Give feedback.
All reactions