Last active
June 27, 2021 09:03
-
-
Save Garciat/f27148cf8c245bb1550da0b730daa883 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
abstract class TySub<A, B> | |
{ | |
public abstract B Cast(A a); | |
} | |
class TySub_Refl<A, B> : TySub<A, B> | |
where A : B | |
{ | |
public override B Cast(A a) | |
{ | |
return a; | |
} | |
} | |
static class TySub_Helper | |
{ | |
public static TySub<A, B> Refl<A, B>() | |
where A : B | |
{ | |
return new TySub_Refl<A, B>(); | |
} | |
} | |
[SharpLab.Runtime.JitGeneric(typeof(string))] | |
class Hello<T> | |
{ | |
public void Store(TySub<T, ICloneable> ts, T value) | |
{ | |
var c = ts.Cast(value); | |
c.Clone(); | |
} | |
} | |
public static class C | |
{ | |
public static void M() | |
{ | |
var hello = new Hello<String>(); | |
hello.Store(new TySub_Refl<string, ICloneable>(), "what"); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package example.playground.sub; | |
import lombok.Value; | |
import java.io.Serializable; | |
class Example<T> { | |
void store(TySub<T, Serializable> ts, T value) { | |
Serializable ser = ts.cast(value); | |
} | |
} | |
// --- | |
abstract class TySub<A, B> { | |
abstract B cast(A a); | |
static <A extends B, B> TySub<A, B> refl() { | |
return new Refl<>(); | |
} | |
@Value | |
static class Refl<A extends B, B> extends TySub<A, B> { | |
@Override | |
B cast(A a) { | |
return a; | |
} | |
} | |
} | |
// --- | |
public class Program { | |
public static void main(String[] args) { | |
var ex = new Example<String>(); | |
ex.store(TySub.refl(), "hello"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment