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

Added constraints for operators, 'external' constraints for methods

This commit is contained in:
Brian Fiete 2019-11-17 09:28:39 -08:00
parent 00a92dd0a7
commit 8945a906f7
24 changed files with 1561 additions and 509 deletions

View file

@ -0,0 +1,45 @@
using System;
namespace Tests
{
class Generics
{
class ClassA : IDisposable
{
public void Dispose()
{
}
}
static void DoDispose<T>(mut T val) where T : IDisposable
{
val.Dispose();
}
struct Disposer<T>
{
static void UseDispose(IDisposable disp)
{
}
static void DoDisposeA(mut T val) where T : IDisposable
{
val.Dispose();
UseDispose(val);
}
static void DoDisposeB(mut T val) where T : IDisposable
{
val.Dispose();
}
}
[Test]
public static void TestBasics()
{
}
}
}

View file

@ -1,3 +1,5 @@
#pragma warning disable 168
using System;
namespace Tests
@ -23,6 +25,151 @@ namespace Tests
}
}
struct StructB
{
public int mB;
public static StructA operator+(StructA sa, StructB sb)
{
StructA result;
result.mA = sa.mA + sb.mB + 1000;
return result;
}
}
struct StructOp<T, T2> where T : operator T + T2
{
public T DoIt(T val, T2 val2)
{
return val + val2;
}
}
struct StructOp2<T>
{
public T mVal;
public static T operator+<T2>(StructOp2<T> lhs, T2 rhs) where T : operator T + T2
{
return lhs.mVal + rhs;
}
public static T operator|<T2>(StructOp2<T> lhs, T2 rhs) where T : operator implicit T2
{
T temp = rhs;
return temp;
}
public T GetNeg<T2>(T2 val) where T : operator -T2
{
return -val;
}
public T GetInt() where T : Int
{
return mVal;
}
}
/*struct OuterOp<T>
{
public struct InnerOp<T2>
where T : operator T + T2
where T : operator -T
where T : operator implicit T2
{
public static T Op(T val, T2 val2)
{
return val + val2;
}
public static T Neg(T val)
{
return -val;
}
public static T Cast(T2 val)
{
return val;
}
struct InnerOp2
{
public static T Op(T val, T2 val2)
{
return val + val2;
}
public static T Neg(T val)
{
return -val;
}
public static T Cast(T2 val)
{
return val;
}
}
struct InnerOp3<T3>
{
public static T Op(T val, T2 val2)
{
return val + val2;
}
public static T Neg(T val)
{
return -val;
}
public static T Cast(T2 val)
{
return val;
}
}
}
}
struct OuterOp2<T>
{
public struct InnerOp<T2>
{
}
extension InnerOp<T2>
where T : operator T + T2
where T : operator -T
{
public static T Op(T val, T2 val2)
{
return val + val2;
}
public static T Neg(T val)
{
return -val;
}
}
}*/
public static T Op<T, T2>(T val, T2 val2) where T : operator T + T2
{
return val + val2;
}
public static T Complex<T, T2>(T val, T2 val2)
where T : operator T + T2
where T : operator -T
where T : operator implicit T2
{
T conv = val2;
T result = val + val2;
result = -result;
return result;
}
[Test]
public static void TestBasics()
{
@ -33,6 +180,40 @@ namespace Tests
StructA sa2 = sa0 + sa1;
Test.Assert(sa2.mA == 3);
StructB sb0;
sb0.mB = 11;
StructB sb1;
sb1.mB = 12;
StructA sa3 = sa0 + sb0;
Test.Assert(sa3.mA == 1012);
StructA sa4 = Op(sa0, sb0);
Test.Assert(sa4.mA == 1012);
float val = Op((int32)100, (int16)23);
Test.Assert(val == 123);
int32 i32res = Complex((int32)100, (int16)23);
Test.Assert(i32res == -123);
StructOp<StructA, StructB> sOp;
let sa5 = sOp.DoIt(sa1, sb1);
Test.Assert(sa5.mA == 1014);
StructOp2<int32> sOp2;
sOp2.mVal = 100;
int32 res6 = sOp2 + (int16)40;
Test.Assert(res6 == 140);
int32 res7 = sOp2.GetInt();
Test.Assert(res7 == 100);
/*let oai = OuterOp<float>.InnerOp<int>.Op(1.0f, 100);
Test.Assert(oai == 101.0f);
let oai2 = OuterOp2<float>.InnerOp<int>.Op(2.0f, 200);
Test.Assert(oai2 == 202.0f);*/
}
}
}