Exemple #1
0
        public void HasATypeEqualToTheInferredReturnTypeOfGenericCallableObjects()
        {
            var x = new TypeVariable(123456);

            Type("func([1, 2, 3])", func => Function(new[] { NamedType.Vector(x) }, x)).ShouldEqual(Integer);
            Type("func([true, false])", func => Function(new[] { NamedType.Vector(x) }, x)).ShouldEqual(Boolean);
        }
 public TypeUnifierTests()
 {
     unifier = new TypeUnifier();
     x = new TypeVariable(0);
     y = new TypeVariable(1);
     z = new TypeVariable(2);
 }
Exemple #3
0
        private DataType NormalizeVariable(TypeVariable variable)
        {
            if (substitutions.ContainsKey(variable))
            {
                DataType substitution = substitutions[variable];

                //We cannot simply return substitution, because it may be a complex
                //type containing type variables that have themselves been unified.
                //Instead, we must return the normalized version of that substitution.
                DataType normalizedSubstitution = Normalize(substitution);

                //Optimization: We could just return normalizedSubstitution, but if
                //If normalizing the substitution provided a better answer than the substitution
                //itself, we might as well store that better answer as the substitution going
                //forward.  This helps by shortening long chains so that they don't have to be
                //traversed again.
                if (normalizedSubstitution != substitution)
                    substitutions[variable] = normalizedSubstitution;

                return normalizedSubstitution;
            }
            return variable;
        }
Exemple #4
0
 public override bool Contains(TypeVariable typeVariable)
 {
     return(genericArguments.Any(genericArgument => genericArgument.Contains(typeVariable)));
 }
Exemple #5
0
 public override bool Contains(TypeVariable typeVariable)
 {
     return false;
 }
Exemple #6
0
 public override bool Contains(TypeVariable typeVariable)
 {
     return genericArguments.Any(genericArgument => genericArgument.Contains(typeVariable));
 }
 public TypeVariableTests()
 {
     a = new TypeVariable(0);
     b = new TypeVariable(1);
 }
Exemple #8
0
 public override bool Contains(TypeVariable typeVariable)
 {
     return(false);
 }
Exemple #9
0
 public void CanBeEitherGenericOrNonGeneric()
 {
     TypeVariable.CreateGeneric().IsGeneric.ShouldBeTrue();
     TypeVariable.CreateNonGeneric().IsGeneric.ShouldBeFalse();
 }
Exemple #10
0
        public void CanPerformTypeVariableSubstitutions()
        {
            var a = new TypeVariable(0);
            var b = new TypeVariable(1);

            var replaceAWithInteger = new Dictionary<TypeVariable, DataType> { { a, NamedType.Integer } };
            var replaceBWithA = new Dictionary<TypeVariable, DataType> { { b, a } };
            var replaceBoth = new Dictionary<TypeVariable, DataType> { { a, NamedType.Integer }, { b, a } };

            var concrete = Create("A", Create("B"));
            concrete.ReplaceTypeVariables(replaceAWithInteger).ShouldEqual(concrete);
            concrete.ReplaceTypeVariables(replaceBWithA).ShouldEqual(concrete);
            concrete.ReplaceTypeVariables(replaceBoth).ShouldEqual(concrete);

            Create("A", a, b, a).ReplaceTypeVariables(replaceAWithInteger).ShouldEqual(Create("A", NamedType.Integer, b, NamedType.Integer));
            Create("B", b, a, b).ReplaceTypeVariables(replaceAWithInteger).ShouldEqual(Create("B", b, NamedType.Integer, b));

            Create("A", a, b, a).ReplaceTypeVariables(replaceBWithA).ShouldEqual(Create("A", a, a, a));
            Create("B", b, a, b).ReplaceTypeVariables(replaceBWithA).ShouldEqual(Create("B", a, a, a));

            //Unlike the type unification/normlization substitutions, these substitutions are ignorant of
            //chains like { b -> a, a -> int }.
            Create("A", a, b, a).ReplaceTypeVariables(replaceBoth).ShouldEqual(Create("A", NamedType.Integer, a, NamedType.Integer));
            Create("B", b, a, b).ReplaceTypeVariables(replaceBoth).ShouldEqual(Create("B", a, NamedType.Integer, a));
        }
Exemple #11
0
        public void CanFindAllDistinctOccurrencesOfContainedTypeVariables()
        {
            var x = new TypeVariable(0);
            var y = new TypeVariable(1);
            var z = new TypeVariable(2);

            Create("A").FindTypeVariables().ShouldBeEmpty();
            Create("A", Create("B")).FindTypeVariables().ShouldBeEmpty();
            Create("A", x, y, z).FindTypeVariables().ShouldList(x, y, z);
            Create("A", Create("B", x, y), Create("C", y, z)).FindTypeVariables().ShouldList(x, y, z);
        }
Exemple #12
0
        public void CanDetermineWhetherTheTypeContainsASpecificTypeVariable()
        {
            var x = new TypeVariable(12345);

            Create("A").Contains(x).ShouldBeFalse();
            Create("A", x).Contains(x).ShouldBeTrue();
            Create("A", Create("B", x)).Contains(x).ShouldBeTrue();
        }
Exemple #13
0
        public void PerformsTypeVariableSubstitutionsAgainstNonGenericTypesByPerformingNoChanges()
        {
            var @class = "class Foo { int Square(int x) {x*x} }".ParseClass();

            var foo = new NamedType(@class, new TypeRegistry());

            var a = new TypeVariable(0);
            var replaceAWithInteger = new Dictionary<TypeVariable, DataType> { { a, NamedType.Integer } };

            var fooAfterSubstitutions = (NamedType)foo.ReplaceTypeVariables(replaceAWithInteger);

            fooAfterSubstitutions.ShouldBeSameAs(foo);
        }
Exemple #14
0
 public void CanNeverBeAGenericTypeDefinition()
 {
     TypeVariable.CreateGeneric().IsGenericTypeDefinition.ShouldBeFalse();
     TypeVariable.CreateNonGeneric().IsGenericTypeDefinition.ShouldBeFalse();
 }
Exemple #15
0
 public override bool Contains(TypeVariable typeVariable)
 {
     return typeVariable == this;
 }
Exemple #16
0
 public abstract bool Contains(TypeVariable typeVariable);
Exemple #17
0
 public override bool Contains(TypeVariable typeVariable)
 {
     return(typeVariable == this);
 }
Exemple #18
0
 public TypeVariableTests()
 {
     a = new TypeVariable(0);
     b = new TypeVariable(1);
 }