From 4cb8e9ecfa85069a6046ccc9bd469c78a7b4d686 Mon Sep 17 00:00:00 2001 From: ig Date: Wed, 1 Mar 2023 10:38:46 +0100 Subject: [PATCH] add more operators to Percent --- csharp/Lib/Units/Percent.cs | 20 ++++++++++++++++++++ csharp/Lib/Units/State.cs | 3 ++- 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/csharp/Lib/Units/Percent.cs b/csharp/Lib/Units/Percent.cs index a358c438f..577dec8a0 100644 --- a/csharp/Lib/Units/Percent.cs +++ b/csharp/Lib/Units/Percent.cs @@ -10,6 +10,7 @@ public readonly struct Percent public Percent(Decimal value) => Value = value; // not generated + // TODO: generate? public Decimal Value { get; } public override String ToString() => Value + Unit; @@ -20,4 +21,23 @@ public readonly struct Percent // parallel public static Percent operator |(T left, T right) => new T((left.Value + right.Value) / 2m); + + // compare + public static Boolean operator ==(T left, T right) => left.Value == right.Value; + public static Boolean operator !=(T left, T right) => left.Value != right.Value; + public static Boolean operator > (T left, T right) => left.Value > right.Value; + public static Boolean operator < (T left, T right) => left.Value < right.Value; + public static Boolean operator >=(T left, T right) => left.Value >= right.Value; + public static Boolean operator <=(T left, T right) => left.Value <= right.Value; + + // conversion + public static implicit operator T(Decimal d) => new T(d); + public static implicit operator T(Double d) => new T((Decimal)d); + public static implicit operator T(Int32 i) => new T(i); + public static implicit operator Decimal(T t) => t.Value; + + // equality + public Boolean Equals(T other) => Value == other.Value; + public override Boolean Equals(Object? obj) => obj is T other && Equals(other); + public override Int32 GetHashCode() => Value.GetHashCode(); } \ No newline at end of file diff --git a/csharp/Lib/Units/State.cs b/csharp/Lib/Units/State.cs index ecabbd034..3060186de 100644 --- a/csharp/Lib/Units/State.cs +++ b/csharp/Lib/Units/State.cs @@ -13,11 +13,12 @@ public readonly struct State } public State(params String[] values) : this((IReadOnlyList)values){} - public State(params State[] states) : this(states.SelectMany(s => s.Values).ToList()){} + public State(params State[] states) : this((IReadOnlyList)states.SelectMany(s => s.Values).ToList()){} public static implicit operator State(String s) => new State(s); public static implicit operator State(Enum e) => new State(e.ToString()); public static implicit operator State(Boolean s) => new State(s.ToString()); + public static implicit operator State(List s) => new State((IReadOnlyList)s); public static State operator |(State left, State right) => new State(left, right);