示例#1
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);
        }
示例#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 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);
        }