QuickCast to Delegates

Tango.Functional.QuickDelegateCast

this static class contains methods to transform a named method to its respectively delegate Func or Action.

It turns way more easily to uses the extensions to Curry and Partial Application.

Methods

Name

Parameters

Returns

Description

F

Func<TResult> function

Func<TResult>

Casts a function as a delegate

F

Func<T, TResult> function

Func<T, TResult>

Casts a function as a delegate

F

Func<T, T2, TResult> function

Func<T, T2, TResult>

Casts a function as a delegate

F

Func<T, T2, T3, TResult> function

Func<T, T2, T3, TResult>

Casts a function as a delegate

F

Func<T, T2, TResult> function

Func<T, T2, TResult>

Casts a function as a delegate

A

Action function

Action

Casts a function as a delegate

A

Action<T> function

Action<T>

Casts a function as a delegate

A

Action<T, T2> function

Action<T, T2>

Casts a function as a delegate

A

Action<T, T2, T3> function

Action<T, T2, T3>

Casts a function as a delegate

A

Action<T, T2, T3, T4> function

Action<T, T2, T3, T4>

Casts a function as a delegate

Usage

The several overloads avaliable in this class can be used to cast a named function into its respectively delegate.

Unfortunately, because of the language limitations is not possible (until now) treat an named function directly as an delegate.

See the code bellow:

int SampleAdd(int value1, int value2)
    => value1 + value2;

SampleAdd.Curry();
SampleAdd.PartialApply(1);

The attemp to uses Curry and PartialApply will causes an compile error, because SampleAdd isa method and can't be used in this specific context.

To uses these extensions is necessary to cast the named function into a delegate.

Func<int, int, int> sampleAddAsDelegate = SampleAdd;

sampleAddAsDelegate.Curry();
sampleAddAsDelegate.PartialApply(1);

With this class, is possible to create a more quick version of this cast.

To do this inline cast, you need to follow the steps bellow:

  1. Add a command of using static Tango.Functional.QuickDelegateCast;

  2. Uses the correct overload described in Methods subsection.

using static Tango.Functional.QuickDelegateCast;

int SampleAdd(int value1, int value2)
    => value1 + value2;

F<int, int, int>(SampleAdd).Curry();
F<int, int, int>(SampleAdd).PartialApply(1);

Through the F() and A() sintax is possible to cast a named function to its respectively delegate and use its extensions.

The fundamentals about Currying and Partial Application can be find in Fundamentals > Currying and Partial Application.

Last updated