C
C#8mo ago
popcorn

Delegate function with params

Hello, is it possible to somehow have "named" parameters in the delegate function? Right now I have:
public delegate bool ValidatorF<T>(params T[] args);

// Usage
public static class IntValidator
{
public static ValidatorF<int> IsExact = args => args[0] == args[1];
}
public delegate bool ValidatorF<T>(params T[] args);

// Usage
public static class IntValidator
{
public static ValidatorF<int> IsExact = args => args[0] == args[1];
}
But that way you have to remember that the first argument is the actual number and the second argument is the expected value for example (In this case it doesn't matter, but let's say we have 3+ args..) My question is: Is it possible to have the usage like this somehow? Or using some other approach?
public static ValidatorF<int> IsExact = (val, expected)=> val == expected;
public static ValidatorF<int> IsExact = (val, expected)=> val == expected;
14 Replies
Pobiega
Pobiega8mo ago
Change the delegate to not take params T[] args?
popcorn
popcornOP8mo ago
I need to take the function from user and I only know that the return type is bool and that any number of params is T type. But I don't know how many params
Pobiega
Pobiega8mo ago
okay, then the answer is no
popcorn
popcornOP8mo ago
Ah okay. Then this must be enough 😄
Pobiega
Pobiega8mo ago
you can ofc do somethng like
public delegate bool ValidatorF<T>(params T[] args);

// Usage
public static class IntValidator
{
public static ValidatorF<int> IsExact = args => IsExactImpl(args[0], args[1]);

private static bool IsExactImpl(int actual, int expected)
{
return actual == expected;
}
}
public delegate bool ValidatorF<T>(params T[] args);

// Usage
public static class IntValidator
{
public static ValidatorF<int> IsExact = args => IsExactImpl(args[0], args[1]);

private static bool IsExactImpl(int actual, int expected)
{
return actual == expected;
}
}
, but that doesnt help the external usage
popcorn
popcornOP8mo ago
Yeah, I'm thinking about just supporting limited number of parameters, just as it is with the generic Func<> where you can only have up to 16 params
Pobiega
Pobiega8mo ago
sure
popcorn
popcornOP8mo ago
Like this:
public static Func<int, int, bool> IsExact2 = (val, expected) => val == expected;

public static Func<int, int, int, bool> IsInBounds = (val, upperBound, lowerBound) => lowerBound >= val && val <= upperBound;
public static Func<int, int, bool> IsExact2 = (val, expected) => val == expected;

public static Func<int, int, int, bool> IsInBounds = (val, upperBound, lowerBound) => lowerBound >= val && val <= upperBound;
Pobiega
Pobiega8mo ago
mhm thats much better, if you ask me
popcorn
popcornOP8mo ago
But then I would have to overload x number of times the generic method. So I would have:
public OptionBuilder<T> ValidationFunc(Func<T, T, bool> validator) { // Save the func to variable }

public OptionBuilder<T> ValidationFunc(Func<T, T, T, bool> validator) { // Save the func to variable }
public OptionBuilder<T> ValidationFunc(Func<T, T, bool> validator) { // Save the func to variable }

public OptionBuilder<T> ValidationFunc(Func<T, T, T, bool> validator) { // Save the func to variable }
And I then I would have to somehow decide which function was actually provided and call that So I would have X number of variables with these functions I guess?
Pobiega
Pobiega8mo ago
yeah btw Func<T, bool> is the same as Predicate<T>
popcorn
popcornOP8mo ago
Cool, didn't know about that one. Thanks I guess sacrificing the complexity of the "library" is better for the comfort of user defined functions.
Pobiega
Pobiega8mo ago
yes, absolutely thats half the point of libraries 🙂
popcorn
popcornOP8mo ago
Okay, thank you for your opinion ❤️
Want results from more Discord servers?
Add your server