add more operators to Percent

This commit is contained in:
ig 2023-03-01 10:38:46 +01:00
parent d2e4f93fd1
commit 4cb8e9ecfa
2 changed files with 22 additions and 1 deletions

View File

@ -10,6 +10,7 @@ public readonly struct Percent
public Percent(Decimal value) => Value = value; public Percent(Decimal value) => Value = value;
// not generated // not generated
// TODO: generate?
public Decimal Value { get; } public Decimal Value { get; }
public override String ToString() => Value + Unit; public override String ToString() => Value + Unit;
@ -20,4 +21,23 @@ public readonly struct Percent
// parallel // parallel
public static Percent operator |(T left, T right) => new T((left.Value + right.Value) / 2m); 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();
} }

View File

@ -13,11 +13,12 @@ public readonly struct State
} }
public State(params String[] values) : this((IReadOnlyList<String>)values){} public State(params String[] values) : this((IReadOnlyList<String>)values){}
public State(params State[] states) : this(states.SelectMany(s => s.Values).ToList()){} public State(params State[] states) : this((IReadOnlyList<String>)states.SelectMany(s => s.Values).ToList()){}
public static implicit operator State(String s) => new State(s); 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(Enum e) => new State(e.ToString());
public static implicit operator State(Boolean s) => new State(s.ToString()); public static implicit operator State(Boolean s) => new State(s.ToString());
public static implicit operator State(List<String> s) => new State((IReadOnlyList<String>)s);
public static State operator |(State left, State right) => new State(left, right); public static State operator |(State left, State right) => new State(left, right);