Skip to content

Instantly share code, notes, and snippets.

@Garciat
Last active June 27, 2021 09:03
Show Gist options
  • Save Garciat/f27148cf8c245bb1550da0b730daa883 to your computer and use it in GitHub Desktop.
Save Garciat/f27148cf8c245bb1550da0b730daa883 to your computer and use it in GitHub Desktop.
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");
}
}
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