using System; namespace BITKit { public interface IOptional { bool Allow { get; } object Value { get; } } public interface IOptional { bool Allow { get; set; } T Value { get; set; } T IfNotAllow(T value); } [System.Serializable] public class Optional : IOptional { #if NET5_0_OR_GREATER bool allow; T value; #else [UnityEngine.SerializeField] bool allow; [UnityEngine.SerializeField] T value; #endif public bool Allow { get => allow; set => allow = value;} public T Value { get => value; set=>this.value=value; } public T IfNotAllow(T other) { return Allow ? Value : other; } public T IfNotAllow(Func other) { return Allow ? Value : other.Invoke(); } public void SetValueThenAllow(T newValue) { value = newValue; allow = true; } public void Clear() { Allow = false; value = default; } public static implicit operator T(Optional self) { return self.value; } } }