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?
We use the ternary operator in C to run one code when the condition is true and another code when the condition is false. For example, (age >= 18) ? printf("Can Vote") : printf("Cannot Vote");
The conditional (ternary) operator is the only JavaScript operator that takes three operands: a condition followed by a question mark ( ? ), then an expression to execute if the condition is truthy followed by a colon ( : ), and finally the expression to execute if the condition is falsy.
A ternary operator takes three major arguments: The comparison argument. The result upon a false comparison. The result upon a true comparison.
It's a good practice to use the ternary operator when it makes the code easier to read. If the logic contains many if...else statements, you should avoid using the ternary operators.
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#.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With