开发者

Can I create ternary operators in C#?

I want to create a ternary operator for a < b < c which is a < b && b < c. or any other option you can think of that a < b > c and so on... I am a fan of my 开发者_如何学运维own shortform and I have wanted to create that since I learned programming in high school.

How?


Don't believe the haters ;)

You can do this in C#. Here's an example implementation — I based the chaining off the way that Icon does theirs... if a comparison succeeds, the result is that of the right parameter, otherwise a special "failed" result is returned.

The only extra syntax you need to use is the call to Chain() after the first item.

class Program
{
    static void Main(string[] args)
    {
        if (2.Chain() < 3 < 4)
        {
            Console.WriteLine("Yay!");
        }
    }
}

public class Chainable<T> where T : IComparable<T>
{
    public Chainable(T value)
    {
        Value = value;
        Failed = false;
    }

    public Chainable()
    {
        Failed = true;
    }

    public readonly T Value;
    public readonly bool Failed;

    public static Chainable<T> Fail { get { return new Chainable<T>(); } }

    public static Chainable<T> operator <(Chainable<T> me, T other)
    {
        if (me.Failed)
            return Fail;

        return me.Value.CompareTo(other) == -1
                   ? new Chainable<T>(other)
                   : Fail;
    }

    public static Chainable<T> operator >(Chainable<T> me, T other)
    {
        if (me.Failed)
            return Fail;

        return me.Value.CompareTo(other) == 1
                   ? new Chainable<T>(other)
                   : Fail;
    }

    public static Chainable<T> operator ==(Chainable<T> me, T other)
    {
        if (me.Failed)
            return Fail;

        return me.Value.CompareTo(other) == 0
                   ? new Chainable<T>(other)
                   : Fail;
    }

    public static Chainable<T> operator !=(Chainable<T> me, T other)
    {
        if (me.Failed)
            return Fail;

        return me.Value.CompareTo(other) != 0
                   ? new Chainable<T>(other)
                   : Fail;
    }

    public static bool operator true(Chainable<T> me)
    {
        return !me.Failed;
    }

    public static bool operator false(Chainable<T> me)
    {
        return me.Failed;
    }

    public override bool Equals(object obj)
    {
        return Value.Equals(obj);
    }

    public override int GetHashCode()
    {
        return Value.GetHashCode();
    }
}

public static class ChainExt
{
    public static Chainable<T> Chain<T>(this T value) where T : IComparable<T>
    {
        return new Chainable<T>(value);
    }
}


Sorry, you cannot create your own operators in C#.

You could use extension methods to enable a fluent syntax like

bool f = b.IsBetween(a, c);

Or, if you were being extremely clever, you could do:

bool f = a.IsLessThan(b).IsLessThan(c);

doing so is tricky, but possible. (Hint: define a custom object that IsLessThan returns that tracks its bounds and understands how it is combined with other instances of the object. Essentially this is how LINQ-to-SQL works with regard to combining Where, Select, and so on.)

But you cannot define your own operator syntaxes in C#.

If you are interested in languages where you can define your own operators, you might consider looking into F#.


You can't do that. You can only implement existing operators, and there is no ternary .<.<. operator in C#.

Besides, such an operator would be ambiguous with existing comparing operators. For example, would the expression a == b == c == d mean ((a == b) == c) == d or (a == b == c) == d?


Have you tried searching for ternary operators in google to see if something already exists?


No. Not in C#. Language extension in this way is not available in any current version of C#, but Luca Bolognese pointed to using the compiler as a service and some functionality which could permit extending the language in such a way here: http://microsoftpdc.com/Sessions/FT11.


If you want to do that for the primitive datatypes, you're out of luck. C# doesn't support adding operators to those.

In your own datatypes it is possible to return a special datatype which stores the intermediate result and the comparisson result. However, I suggest you just stick to the c# language - if you really want the a < b < c operator style, switch to Python.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜