0

Let's have a function:

Func<argsFoo, resultFoo> foo= x=>new resultFoo(x);
Func<argsBar, resultBar> bar= x=>new resultBar(x);
Func<argsBaz, resultBaz> baz= x=>new resultBaz(x);

I want to create a complex function which uses all this functions declared above.

Like this:

Func
<
    Func<argsFoo, resultFoo>,
    Func<argsBar, resultBar>,
    Func<argsBaz,resultBaz>,
    fooBarBazResult
> fooBarBaz=...

The point is such declaration is killing the readability of the programm. Type inference is not working in this case.

Question: can I use something like this?

FooBarBaz<typeof(foo),typeof(bar)>,typeof(baz)>>

I have tried and answer is no. May be anyone has another solution to make shorter the composed function declarations?

3
  • 4
    This looks strange. The last generic parameter of a Func is the return type. You have three Funcs with three different return types and still, you are trying to combine them with a Func that takes the first to of them as input and returns a Func that looks like the third? What are you really trying to do here? Commented Jul 20, 2013 at 11:58
  • 1
    Not related to what you ask but the lambda (x)=>{return new resultFoo(x);} (semicolon missing in your code!) can be simplified a bit, to x=>new resultFoo(x). Commented Jul 20, 2013 at 12:03
  • Daniel you are right I should correct the mistake in example. Commented Jul 20, 2013 at 12:10

2 Answers 2

1

You can always declare your own delegate that will be describing your methods:

public delegate resultMy MyDelegate(argsMy arg)

and use the shorter name MyDelegate

Func<FooDelagate, BatDelegate, BazDelegate>

and even that delegate you can name to simplify you code.

Sign up to request clarification or add additional context in comments.

2 Comments

Delegates are good in more static-like code such as method declarations. But anonymous functions are designed to be light weight (I suppose) and using delegates in this case makes code more robust as it used to be.
@gleb.kudr - Func<T, R> is a delegate type, just one that is defined in the BCL. You can only pass lambdas or anonymous functions around as delegate instances.
0

Well, you could add a using statement at the top of your file that simplifies that:

using SomeName = Func<Func<ArgsFoo, ResultFoo>, Func<ArgsBar, ResultBar>, Func<ArgsBaz, ResultBaz>>;

And then you couls just use it as this:

SomeName myFunc = //the func

2 Comments

On this case I can use delegates. The question is how not to use them :)
@gleb.kudr The other option is reflection, which would make it far too complex.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.