#pragma warning disable 168 using System; namespace Tests { class ConstExprs { enum EnumA { A, B, C } class ClassA where TSize : const int { public int GetVal() { return TSize; } } class ClassB where TSize : const int8 { ClassA mVal = new ClassA() ~ delete _; var mVal2 = new ClassA() ~ delete _; public int GetVal() { return mVal.GetVal(); } public int GetVal2() { return mVal2.GetVal(); } } class ClassC where TEnum : const EnumA { public int Test() { EnumA ea = TEnum; if (TEnum == .A) { return 1; } return 0; } } struct TestRangedArray where TRange : const var { [OnCompile(.TypeInit), Comptime] static void TypeInit() { if (TRange is var) return; int rangeStart = 0; int rangeEnd = 0; if (ClosedRange range = TRange as ClosedRange?) { rangeStart = range.Start; rangeEnd = range.End+1; } else if (Range range = TRange as Range?) { rangeStart = range.Start; rangeEnd = range.End; } else { Compiler.EmitTypeBody(typeof(Self), scope $""" public const String cError = "Invalid type: {TRange}"; """); return; } Compiler.EmitTypeBody(typeof(Self), scope $""" public const int cRangeStart = {rangeStart}; public const int cRangeEnd = {rangeEnd}; public T[{rangeEnd-rangeStart}] mData; """); } } struct SpecialId : int { public const Self CONST = (.)(int)(void*)(char8*)"ABC"; public static Self operator implicit(String str) => (.)(int)(void*)(char8*)str; } public static void TestStr(SpecialId specialId) { char8* ptr = (.)(void*)(int)specialId; StringView sv = .(ptr); Test.Assert(sv == "ABC"); } [Test] public static void TestBasics() { ClassB cb = scope .(); Test.Assert(cb.GetVal() == 123); Test.Assert(cb.GetVal2() == 223); ClassB cb2 = scope .(); Test.Assert(cb2.GetVal() == -45); Test.Assert(cb2.GetVal2() == 55); ClassC cc = scope .(); Test.Assert(cc.Test() == 1); Test.Assert(TestRangedArray.cRangeEnd - TestRangedArray.cRangeStart == 7); Test.Assert(TestRangedArray.cError == "Invalid type: -3...^1"); TestStr(.CONST); TestStr("ABC"); } } }