This project is read-only.

Pattern Matching Overload

Topics: C# Language Design
Jun 9, 2014 at 1:14 PM
It is similar to pattern matching of Haskell.
This is a sample.
static Complex[] SolveQuadraticEquation(bool allowComplex@true, double a, double b, double c)
{
    Complex[] result = new Complex[2];
    //Do something
    return result;
}
static double[] SolveQuadraticEquation(bool allowComplex@false, double a, double b, double c)
{
    double[] result = new double[2];
    //Do something
    return result;
}
///<summary>foo</summary>
void Checkup(Animals animal@Animals.Elephant, double weight)
{
   WriteLine("{0}t elephant!", weight);
}
///<summary>bar</summary>
void Checkup(Animals animal@Animals.Dog, double weight)
{
   WriteLine("{0}kg dog!", weight);
}
///<summary>foofoofoo</summary>
void Checkup(Animals animal@Animals.Fish, double weight)
{
    WriteLine("{0}g fish!", weight);
}
///<summary>barbarbar</summary>
void Checkup(Animals animal, double weight) //default
{
    WriteLine("{0}", weight);
}
This syntax allow us to use Intellisense more conveniently and more type-safe.
And switch-case of argments is not beautiful, i think.
Jun 10, 2014 at 8:15 PM
I'm not sure how one could define overloading rules to avoid ambiguity, unless one required that for every fixed-parameter-value overload there must be a fallback available with a non-fixed parameter value, and allowed the fallback to be used in cases of ambiguity. On the other hand, I could see some nice usefulness to being able to overload based upon whether a parameter is a compile-time constant, especially if a method overloaded in such fashion could return a value which could in turn be used as a compile-time constant.