1
0
Fork 0
mirror of https://github.com/beefytech/Beef.git synced 2025-06-10 12:32:20 +02:00

Improvements to interfaces: extensions, better generics, statics

This commit is contained in:
Brian Fiete 2020-05-27 09:46:09 -07:00
parent bff1d657cc
commit 6cb2df65a6
12 changed files with 322 additions and 68 deletions

View file

@ -123,5 +123,115 @@ namespace Tests
Test.Assert(UseIA2(cc) == 70);
Test.Assert(UseIA2(cca) == 70);
}
////
interface IFaceD<T>
{
T GetVal();
T Add<T2>(T lhs, T2 rhs) where T : operator T + T2
{
return lhs + rhs;
}
static T SMethod(T val);
static T SMethod2(T val)
{
return val;
}
}
extension IFaceD<T>
{
T GetVal2()
{
return GetVal();
}
}
class ClassD : IFaceD<int16>
{
public int16 GetVal()
{
return 123;
}
public int16 Add<T2>(int16 lhs, T2 rhs) where int16 : operator int16 + T2
{
return lhs + rhs + 1000;
}
public static int16 SMethod(int16 val)
{
return val + 2000;
}
}
class ClassE : IFaceD<int16>
{
public int16 GetVal()
{
return 234;
}
public static int16 SMethod(int16 val)
{
return val + 3000;
}
public static int16 SMethod2(int16 val)
{
return val + 4000;
}
}
public static int IDAdd<T>(T val) where T : IFaceD<int16>
{
return val.Add((int16)23, (int8)100);
}
public static T2 SGet<T, T2>(T val) where T : IFaceD<T2>
{
return T.SMethod(val.GetVal());
}
public static T2 SGet2<T, T2>(T val) where T : IFaceD<T2>
{
return T.SMethod2(val.GetVal());
}
[Test]
public static void TestDefaults()
{
ClassD cd = scope .();
IFaceD<int16> ifd = cd;
int v = ifd.GetVal();
Test.Assert(v == 123);
v = ifd.GetVal2();
Test.Assert(v == 123);
v = IDAdd(cd);
Test.Assert(v == 1123);
v = SGet<ClassD, int16>(cd);
Test.Assert(v == 2123);
v = SGet2<ClassD, int16>(cd);
Test.Assert(v == 123);
ClassE ce = scope .();
ifd = ce;
v = ifd.GetVal();
Test.Assert(v == 234);
v = ifd.GetVal2();
Test.Assert(v == 234);
v = IDAdd(ce);
Test.Assert(v == 123);
v = SGet<ClassE, int16>(ce);
Test.Assert(v == 3234);
v = SGet2<ClassE, int16>(ce);
Test.Assert(v == 4234);
}
}
}

View file

@ -270,5 +270,17 @@ namespace Tests
TestDefaults();
}
public static TTo Convert<TFrom, TTo>(TFrom val) where TTo : operator explicit TFrom
{
return (TTo)val;
}
[Test]
public static void TestConversion()
{
int a = 123;
float f = Convert<int, float>(a);
}
}
}