G
Guest
C++ template use constraint by signature,It's very flexible to programmer but
complex for complier, and at most time programmer can not
get clear error message from complier if error occur. C# generic use
constraint by type,complier is relaxed, but it is very limited to
programmer.Is there a way to get merits of both?
Maybe the following way can achieve this purpose :
//First add a keyword "constrant" to modify class or struct just like
keyword "abstract"
//Used to define constrant, so the meta data can be treat same as common
class.
constrant class TRef : ISomeInterface
{
//Constructors
public TRef();
public TRef(int i);
//Methods
public static bool operator ==(TRef t1, TRef t2);
public static bool operator +(TRef t1, TRef t2);
public void Method1();
public void Method2(TRef t, int i);
//Properties
public int Property1{get;set;}
public int this[int i]{get;}
}
constrant struct TValue: ISomeInterface
{
//Constructors
public TValue();
public TValue(int i);
//Static Methods
public static bool operator ==(TValue t1, TValue t2);
public static bool operator +(TValue t1, TValue t2);
public void Method1();
public void Method2(TValue t, int i);
//Properties
public int Property1{get;set;}
public int this[int i]{get;}
}
//GenericClass use TRef,TValue as normal class,compiler can give clear error
message
public class GenericClass<TRef,TValue>
//Note here use constrant class or struct directly
//"where" statement is not needed now
{
public void sample()
{
//here just treat TRef,TValue as normal class
TRef refObj = new TRef();
TRef refObj2 = new TRef(1);
TRef refObj3 = refObj + refObj2;
TValue valueObj = new TValue(100);
TValue valueObj2 = new TValue(10);
if (valueObj == valueObj2)
{
valueObj.Method1();
valueObj2.Method2();
}
int i=refObj3.Property1;
int j=valueObj2[0];
}
}
public class UseGenericClass
{
public void Test()
{
//Compiler will check:
//RealClass includes all signatures of TRef, but do not force
RealClass inherited from TRef;
//RealValue includes all signatures of TValue
GenericClass<RealClass,RealValue> obj=new
GenericClass<RealClass,RealValue>();
}
}
complex for complier, and at most time programmer can not
get clear error message from complier if error occur. C# generic use
constraint by type,complier is relaxed, but it is very limited to
programmer.Is there a way to get merits of both?
Maybe the following way can achieve this purpose :
//First add a keyword "constrant" to modify class or struct just like
keyword "abstract"
//Used to define constrant, so the meta data can be treat same as common
class.
constrant class TRef : ISomeInterface
{
//Constructors
public TRef();
public TRef(int i);
//Methods
public static bool operator ==(TRef t1, TRef t2);
public static bool operator +(TRef t1, TRef t2);
public void Method1();
public void Method2(TRef t, int i);
//Properties
public int Property1{get;set;}
public int this[int i]{get;}
}
constrant struct TValue: ISomeInterface
{
//Constructors
public TValue();
public TValue(int i);
//Static Methods
public static bool operator ==(TValue t1, TValue t2);
public static bool operator +(TValue t1, TValue t2);
public void Method1();
public void Method2(TValue t, int i);
//Properties
public int Property1{get;set;}
public int this[int i]{get;}
}
//GenericClass use TRef,TValue as normal class,compiler can give clear error
message
public class GenericClass<TRef,TValue>
//Note here use constrant class or struct directly
//"where" statement is not needed now
{
public void sample()
{
//here just treat TRef,TValue as normal class
TRef refObj = new TRef();
TRef refObj2 = new TRef(1);
TRef refObj3 = refObj + refObj2;
TValue valueObj = new TValue(100);
TValue valueObj2 = new TValue(10);
if (valueObj == valueObj2)
{
valueObj.Method1();
valueObj2.Method2();
}
int i=refObj3.Property1;
int j=valueObj2[0];
}
}
public class UseGenericClass
{
public void Test()
{
//Compiler will check:
//RealClass includes all signatures of TRef, but do not force
RealClass inherited from TRef;
//RealValue includes all signatures of TValue
GenericClass<RealClass,RealValue> obj=new
GenericClass<RealClass,RealValue>();
}
}