0

I receiving the following error: "Using generic type 'Func<TResult>' requires 1 type arguments". It occurs when I attempt to define a dictionary which maps strings to delegate functions.

The dictionary looks like this:

Dictionary<string, string> builtInFunctions = new Dictionary<string, Func<Expression, Dictionary<string, string>, Dictionary<string, Value>, Dictionary<string, Token>, Dictionary<string, Cube>, Result>>()
{
    {"ToString", ToString}
};

Result ToString(
    Expression expression,
    Dictionary<string, string> env, 
    Dictionary<string, Value> store, 
    ref Dictionary<string, Token> tokenEnv, 
    ref Dictionary<string, Cube> cubeEnv
) {
    // implemented ToString function
}

And the error is occurring on this partof the code:

Func<Expression, Dictionary<string, string>, Dictionary<string, Value>, Dictionary<string, Token>, Dictionary<string, Cube>, Result>

And it still appears if I use different simpler types for it, for instance:

Func<int, int, int, int, int, int>

Can delegate functions only take 4 arguments, or is there a way around this?

Oliver Hare
  • 29
  • 1
  • 7

1 Answers1

0

Your ToString function mustn't have ref parameters.

They are not defined in your Func and they can't be.

Look at this question for more insight.

You might have error in your code, because declaring a Func with four or five arguments shouldn't be a problem.

This code works perfectly fine with me:

using System;
using System.Collections.Generic;

public class Expression{}
public class Value{}
public class Token{}
public class Cube{}
public class Result{
    public int result = 11;
}

namespace MyNameSpace{
    using MyGenericFunc = Func<Expression, Dictionary<string, string>, Dictionary<string, Value>, Dictionary<string, Token>, Dictionary<string, Cube>, Result>;


    public class Program
    {
        public static void Main()
        {
            Expression expression = new Expression();
            Dictionary<string, string> env = new Dictionary<string, string>(); 
            Dictionary<string, Value> store = new Dictionary<string, Value>();
            Dictionary<string, Token> tokenEnv = new Dictionary<string, Token>();
            Dictionary<string, Cube> cubeEnv = new Dictionary<string, Cube>();

            // normal call
            Console.WriteLine("When calling the method directly: " + ToStringFunc(expression, env, store, tokenEnv, cubeEnv).result);

            // call via func and dictionary
            MyGenericFunc retrievedFunc;
            builtInFunctions.TryGetValue("ToString", out retrievedFunc);
            Console.WriteLine("When calling the method retrieved from dictionary: " + retrievedFunc.Invoke(expression, env, store, tokenEnv, cubeEnv).result);
        }

        static Dictionary<string, MyGenericFunc> builtInFunctions = new Dictionary<string, MyGenericFunc>()
        {
            {"ToString", ToStringFunc}
        };

        static Result ToStringFunc(
            Expression expression,
            Dictionary<string, string> env, 
            Dictionary<string, Value> store, 
            Dictionary<string, Token> tokenEnv, 
            Dictionary<string, Cube> cubeEnv
        ) {
            return new Result();
        }
    }
}

You can test it quickly here.

halfer
  • 19,824
  • 17
  • 99
  • 186
Benjamin Basmaci
  • 2,247
  • 2
  • 25
  • 46
  • Sure, but this doesn't answer the question with regards to the error I'm asking about. – Oliver Hare Feb 23 '19 at 23:35
  • To clarify my last comment, I can see a different way to accomplish what I want with this answer, but it doesn't clarify on Func declarations giving an error if you define a certain number of parameters. – Oliver Hare Feb 23 '19 at 23:46
  • @OliverHare Then you must have some syntax error maybe? I edited my answer and provide example code that works without the `ref` parameters. – Benjamin Basmaci Feb 23 '19 at 23:48
  • Looks like you are right. Sorry about the earlier confusion, I'm using VScode with the C# extension and it is throwing an error when ever I have a `Func<>` with more than 5 types, but testing your code, I see it still works when run. It must be an issue with the C# extension misinterpretting the syntax of a Func if it has too many parameters. Thanks. – Oliver Hare Feb 24 '19 at 00:30