Exemple #1
0
        protected override Type ResolveInternal(Context ctx, bool mustReturn)
        {
            var argTypes = new List <Type>();

            foreach (var curr in Arguments)
            {
                if (curr.IsVariadic)
                {
                    Error(CompilerMessages.VariadicArgumentLambda);
                }

                var type = curr.GetArgumentType(ctx);
                argTypes.Add(type);

                if (type == typeof(UnspecifiedType))
                {
                    MustInferArgTypes = true;
                }
            }

            if (MustInferArgTypes)
            {
                return(FunctionalHelper.CreateLambdaType(argTypes.ToArray()));
            }

            Body.Scope.RegisterArguments(ctx, false, Arguments);

            var retType = Body.Resolve(ctx);

            return(FunctionalHelper.CreateDelegateType(retType, argTypes.ToArray()));
        }
        public void CreateLambdaTypeTest()
        {
            Assert.AreEqual(
                typeof(Lambda <int, string, TimeSpan>),
                FunctionalHelper.CreateLambdaType(typeof(int), typeof(string), typeof(TimeSpan))
                );

            Assert.AreEqual(typeof(Lambda <bool>), FunctionalHelper.CreateLambdaType(typeof(bool)));
            Assert.AreEqual(typeof(Func <UnspecifiedType>), FunctionalHelper.CreateLambdaType());

            Assert.Throws <LensCompilerException>(() => FunctionalHelper.CreateLambdaType(new Type[20]));
        }
Exemple #3
0
        protected override Type resolve(Context ctx, bool mustReturn)
        {
            var argTypes = new List <Type>();

            foreach (var curr in Arguments)
            {
                if (curr.IsVariadic)
                {
                    error(CompilerMessages.VariadicArgumentLambda);
                }

                var type = curr.GetArgumentType(ctx);
                argTypes.Add(type);

                if (type == typeof(UnspecifiedType))
                {
                    MustInferArgTypes = true;
                }
            }

            if (MustInferArgTypes)
            {
                return(FunctionalHelper.CreateLambdaType(argTypes.ToArray()));
            }

            Body.Scope.RegisterArguments(ctx, false, Arguments);

            var retType = Body.Resolve(ctx);

            if (_InferredDelegateType != null)
            {
                if (!_InferredReturnType.IsExtendablyAssignableFrom(retType))
                {
                    error(CompilerMessages.LambdaReturnTypeMismatch, _InferredDelegateType.Name, retType.Name, _InferredReturnType.Name);
                }

                return(_InferredDelegateType);
            }

            return(FunctionalHelper.CreateDelegateType(retType, argTypes.ToArray()));
        }