Instantly share code, notes, and snippets.

Embed
What would you like to do?
Method chaining Pattern with generic derivation.C# and Typescript
public class A<T> where T :A<T>{
protected T t;
public T funcA(){
return t;
}
}
public class A:A<A>{
private A(){
this.t =this;
}
public static A create(){
return new A();
}
}
public class B<T>:A<T> where T :B<T>{
public T funcB(){
return t;
}
}
public class B:B<B>{
private B(){
this.t =this;
}
public static B create(){
return new B();
}
}
public class Program{
public static void test(){
var a = A.create().funcA().funcA().funcA();
var b = B.create().funcA().funcB().funcA();
}
}
class A<T extends A<T>>{
protected t:T;
public static ABuilder = class ABuilder extends A<ABuilder>{
private constructor(){
super();
this.t = this;
}
public static create():ABuilder{
return new ABuilder();
}
}
public funcA():T{
return this.t;
}
}
class B<T extends B<T>> extends A<T>{
protected t:T;
public static BBuilder = class BBuilder extends B<BBuilder>{
private constructor(){
super();
this.t = this;
}
public static create():BBuilder{
return new BBuilder();
}
}
public funcB():T{
return this.t;
}
}
let a = A.ABuilder.create().funcA().funcA().funcA();
let b = B.BBuilder.create().funcB().funcA().funcB();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment