示例#1
0
        public void TestSpecializedGenericTypesEquality()
        {
            // class Foo[T](val id: T)
            // class SecondFoo[A](id: Int, val name: A) extends Foo[Int](id)
            RType fooTy = new RType("Foo");
            fooTy.DefineGenericParameters("T");
            RType intFooTy = fooTy.MakeGenericType(IntTy);

            RType secondTy = new RType("SecondFoo");
            secondTy.DefineGenericParameters("A");
            secondTy.BaseType = intFooTy;

            Assert.IsTrue(secondTy.IsGenericType);
            Assert.IsTrue(secondTy.IsGenericTypeDefinition);

            Assert.IsTrue(intFooTy.IsAssignable(secondTy));
        }
示例#2
0
 private static RType CreateArrayType()
 {
     RType arrayType = new RType("Array") {IsArray = true};
     RppGenericParameter genericParameter = arrayType.DefineGenericParameters("A")[0];
     arrayType.DefineConstructor(RMethodAttributes.Public, new[] {new RppParameterInfo("size", IntTy)});
     arrayType.DefineMethod("length", RMethodAttributes.Public, IntTy, new RppParameterInfo[0]);
     arrayType.DefineMethod("apply", RMethodAttributes.Public, genericParameter.Type, new[] {new RppParameterInfo("index", IntTy)},
         new RppGenericParameter[0]);
     arrayType.DefineMethod("update", RMethodAttributes.Public, UnitTy,
         new[] {new RppParameterInfo("index", IntTy), new RppParameterInfo("value", genericParameter.Type)}, new RppGenericParameter[0]);
     return arrayType;
 }
示例#3
0
        public void TestOneGenericContainerWith2DistinctClasses()
        {
            // class List[A]
            // class Apple
            // class Orange
            // listOfApples = new List[Apple]()
            // listOfOranges = new List[Orange]()

            RType listTy = new RType("List");
            listTy.DefineGenericParameters("A");

            RType appleTy = new RType("Apple");
            RType listOfApplesTy = listTy.MakeGenericType(appleTy);

            RType orangeTy = new RType("Orange");
            RType listOfOrangesTy = listTy.MakeGenericType(orangeTy);

            Assert.IsFalse(listOfOrangesTy.IsAssignable(listOfApplesTy));
            Assert.IsFalse(listOfApplesTy.IsAssignable(listOfOrangesTy));

            Assert.IsTrue(listOfOrangesTy.IsAssignable(listOfOrangesTy));
        }
示例#4
0
        public void TestAssigningNothing()
        {
            RType optionTy = new RType("Option");
            RppGenericParameter[] genericParameters = optionTy.DefineGenericParameters("A");
            genericParameters[0].Variance = RppGenericParameterVariance.Covariant;

            RType nothingOptionTy = optionTy.MakeGenericType(NothingTy);
            RType stringOptionTy = optionTy.MakeGenericType(StringTy);

            Assert.IsTrue(stringOptionTy.IsAssignable(nothingOptionTy));
        }
示例#5
0
        public void ExtendingSpecializedGenericAndDefineOneMoreGenericParameter()
        {
            // class Foo[T]
            // class SecondFoo[A] extends Foo[Int]

            // Foo[Int] = SecondFoo[String]
            RType fooTy = new RType("Foo");
            fooTy.DefineGenericParameters("T");

            RType intFooTy = fooTy.MakeGenericType(IntTy);
            RType secondFooTy = new RType("SecondFoo", RTypeAttributes.Class, intFooTy);

            Assert.IsTrue(intFooTy.IsAssignable(secondFooTy));
        }
示例#6
0
        /// <summary>
        /// Creates 2 types, List[Fruit] and List[Apple], where:
        /// <code>
        /// class List[A]
        /// class Fruit
        /// class Apple extends Fruit
        /// </code>
        /// </summary>
        /// <param name="variance">variance type for type argument <code>'A'</code></param>
        /// <param name="listOfFruits"></param>
        /// <param name="listOfApples"></param>
        private static void CreateTypes(RppGenericParameterVariance variance, out RType listOfFruits, out RType listOfApples)
        {
            RType listTy = new RType("List");
            RppGenericParameter[] genericParameters = listTy.DefineGenericParameters("A");
            genericParameters[0].Variance = variance;
            RType fruitTy = new RType("Fruit");

            RType listOfFruitsTy = listTy.MakeGenericType(fruitTy);

            RType appleTy = new RType("Apple", RTypeAttributes.Class, fruitTy);
            listOfApples = listTy.MakeGenericType(appleTy);

            listOfFruits = listOfFruitsTy;
        }
示例#7
0
        public void InflateSimpleGeneric()
        {
            /*
            class Foo[A,B]{
                def get(x: A, y: B): B
            }
            */
            RType fooTy = new RType("Foo");
            RppGenericParameter[] gp = fooTy.DefineGenericParameters("A", "B");
            fooTy.DefineMethod("get", RMethodAttributes.Public, gp[0].Type, new[] {new RppParameterInfo(gp[0].Type), new RppParameterInfo(gp[1].Type)});

            // Foo[Int, Float]
            RType specializedFooTy = fooTy.MakeGenericType(IntTy, FloatTy);
            // get(x: Int) : Float
            RppMethodInfo getMethod = specializedFooTy.Methods[0];
            Assert.AreEqual(IntTy, getMethod.ReturnType);
            Assert.AreEqual(IntTy, getMethod.Parameters[0].Type);
            Assert.AreEqual(FloatTy, getMethod.Parameters[1].Type);
        }
示例#8
0
        public void InflateClassWithGenericBaseType()
        {
            /*
                class Foo[A, B] {
                    def get(x: A) : B
                }
                class Bar[A, B, C] extends Foo[A, B]
                {
                    def map(x: A, y: B) : C
                }
            */
            RType fooTy = new RType("Foo");
            {
                RppGenericParameter[] gp = fooTy.DefineGenericParameters("X", "Y");
                fooTy.DefineMethod("get", RMethodAttributes.Public, gp[1].Type, new[] {new RppParameterInfo(gp[0].Type)});
            }

            RType barTy = new RType("Bar");
            RppGenericParameter[] barGp = barTy.DefineGenericParameters("A", "B", "C");
            barTy.DefineMethod("map", RMethodAttributes.Public, barGp[2].Type, new[]
            {
                new RppParameterInfo(barGp[0].Type),
                new RppParameterInfo(barGp[1].Type)
            });
            barTy.BaseType = fooTy.MakeGenericType(barGp[0].Type, barGp[1].Type);

            RType specilizedBarTy = barTy.MakeGenericType(IntTy, FloatTy, StringTy);
            Assert.IsNotNull(specilizedBarTy.BaseType);
            IReadOnlyCollection<RType> barGenericArguments = specilizedBarTy.GenericArguments;
            CollectionAssert.AreEqual(new[] {IntTy, FloatTy, StringTy}, barGenericArguments.ToList());
            var fooGenericArguments = specilizedBarTy.BaseType.GenericArguments;
            CollectionAssert.AreEqual(new[] {IntTy, FloatTy}, fooGenericArguments.ToList());
            RppMethodInfo getMethod = specilizedBarTy.BaseType.Methods[0];
            Assert.AreEqual(FloatTy, getMethod.ReturnType);
            Assert.AreEqual(IntTy, getMethod.Parameters[0].Type);
        }
示例#9
0
        private static RType CreateClosureType(int argCount)
        {
            RType closureTy = new RType("Function");
            string[] genericNames = Enumerable.Range(0, argCount).Select(r => $"T{r + 1}").Concat("TResult").ToArray();
            RppGenericParameter[] genericParams = closureTy.DefineGenericParameters(genericNames);
            for (int i = 0; i < genericParams.Length - 1; i++)
            {
                genericParams[i].Variance = RppGenericParameterVariance.Contravariant;
            }

            genericParams[genericParams.Length - 1].Variance = RppGenericParameterVariance.Covariant;
            return closureTy;
        }