Last active
March 9, 2020 12:19
-
-
Save ZacharyPatten/aaf42c8ed8a5c08b69c241920f6d5c5d to your computer and use it in GitHub Desktop.
Inequality syntax in C#
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using System; | |
| using Towel; | |
| using static Towel.Syntax; | |
| namespace ConsoleApp1 | |
| { | |
| class Program | |
| { | |
| static void Main() | |
| { | |
| // valid syntax | |
| { | |
| Console.WriteLine((Inequality<float>) 1 < 2 < 3 < 4 <= 4 < 5 < 6); | |
| Console.WriteLine((Inequality<float>) 6 > 5 > 4 >= 4 > 3 > 2 > 1); | |
| Console.WriteLine((Inequality<float>) 3 < 2 < 1); | |
| Console.WriteLine((Inequality<float>) 1 > 2 > 3); | |
| } | |
| // invalid syntax | |
| { | |
| //// this will not compile (a good thing) | |
| //if ((Inequality<float>) 1) | |
| //{ | |
| // | |
| //} | |
| try | |
| { | |
| Console.WriteLine((Inequality<float>) 1); | |
| } | |
| catch (InequalitySyntaxException) | |
| { | |
| Console.WriteLine("Inequality Syntax Error"); | |
| } | |
| try | |
| { | |
| Inequality<float> a = default; | |
| Console.WriteLine(a < 1); | |
| } | |
| catch (InequalitySyntaxException) | |
| { | |
| Console.WriteLine("Inequality Syntax Error"); | |
| } | |
| } | |
| Console.ReadLine(); | |
| return; | |
| } | |
| } | |
| public struct Inequality<T> | |
| { | |
| private bool Cast; | |
| private T A; | |
| public static implicit operator Inequality<T>(T a) => | |
| new Inequality<T>() | |
| { | |
| Cast = true, | |
| A = a, | |
| }; | |
| public static OperatorValidated.Inequality<T> operator >(Inequality<T> a, T b) => | |
| !a.Cast ? throw new InequalitySyntaxException() : | |
| new OperatorValidated.Inequality<T>(Comparison(a.A, b) == CompareResult.Greater, b); | |
| public static OperatorValidated.Inequality<T> operator <(Inequality<T> a, T b) => | |
| !a.Cast ? throw new InequalitySyntaxException() : | |
| new OperatorValidated.Inequality<T>(Comparison(a.A, b) == CompareResult.Less, b); | |
| public static OperatorValidated.Inequality<T> operator >=(Inequality<T> a, T b) => | |
| !a.Cast ? throw new InequalitySyntaxException() : | |
| new OperatorValidated.Inequality<T>(Comparison(a.A, b) != CompareResult.Less, b); | |
| public static OperatorValidated.Inequality<T> operator <=(Inequality<T> a, T b) => | |
| !a.Cast ? throw new InequalitySyntaxException() : | |
| new OperatorValidated.Inequality<T>(Comparison(a.A, b) != CompareResult.Greater, b); | |
| public override string ToString() => throw new InequalitySyntaxException(); | |
| } | |
| namespace OperatorValidated | |
| { | |
| public struct Inequality<T> | |
| { | |
| private readonly bool Result; | |
| private readonly T A; | |
| internal Inequality(bool result, T a) | |
| { | |
| Result = result; | |
| A = a; | |
| } | |
| public static implicit operator bool(Inequality<T> inequality) => | |
| inequality.Result; | |
| public static Inequality<T> operator >(Inequality<T> a, T b) => | |
| new Inequality<T>(a.Result && Comparison(a.A, b) == CompareResult.Greater, b); | |
| public static Inequality<T> operator <(Inequality<T> a, T b) => | |
| new Inequality<T>(a.Result && Comparison(a.A, b) == CompareResult.Less, b); | |
| public static Inequality<T> operator >=(Inequality<T> a, T b) => | |
| new Inequality<T>(a.Result && Comparison(a.A, b) != CompareResult.Less, b); | |
| public static Inequality<T> operator <=(Inequality<T> a, T b) => | |
| new Inequality<T>(a.Result && Comparison(a.A, b) != CompareResult.Greater, b); | |
| public override string ToString() => Result.ToString(); | |
| } | |
| } | |
| public class InequalitySyntaxException : Exception { } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment