So this was inspired by: this question But doesn't actually answer it: What do you think of:
public class Case<TRes>
{
public Case(bool condition, Func<TRes> result)
{
Condition = condition;
Result = result;
}
public bool Condition {get; private set;}
public Func<TRes> Result { get; private set; }
public static Case<TRes> Default(Func<TRes> value)
{
return new Case<TRes>(true, value);
}
}
public static class Logic
{
public static T Match<T> (params Case<T>[] cases)
{
return cases.First(c => c.Condition).Result();
}
}
Which you could invoke:
int i = Logic.Match
(
new Case<int>(IsCloudy(skyImage), () =>
{
ChangeWeatherIcon(Icons.Clouds);
return 13;
}),
new Case<int>(a < b, () => a),
Case<int>.Default(() => {throw new Exception(); })
);
it's purpose isn't quiet to emulate F#'s Match. (Because F# match doens't actually take conditions, it is a stuctural/pattern match) So really its kind of like the trinary operator, meets the nonOrdinal Select statment.
Anyone got any idea's on how to make it work cleaner? Maybe getting a way to get rid of the new's and the brackets