i made a small timeout struct and wanted to add conversion to make it a bit easier to use:
public struct TimeoutValue<T>
{
public static TimeSpan DefaultTimeout { get; } = TimeSpan.FromSeconds(5);
public DateTime CreationTime { get; set; } = DateTime.UtcNow;
public TimeSpan? CustomTimeout { get; set; } = null;
public T Value { get; set; }
public readonly bool IsTimeouted => DateTime.UtcNow - CreationTime >= (CustomTimeout ?? DefaultTimeout);
public TimeoutValue(T value) => Value = value;
public TimeoutValue(DateTime creationTime, T value) : this(value) => CreationTime = creationTime;
public TimeoutValue(TimeSpan customTimeout, T value) : this(value) => CustomTimeout = customTimeout;
public TimeoutValue(DateTime creationTime, TimeSpan customTimeout, T value) : this(creationTime, value) => CustomTimeout = customTimeout;
public static explicit operator T(TimeoutValue<T> value) => value.Value;
public static implicit operator TimeoutValue<T>(T value) => new(value);
public static implicit operator T?(TimeoutValue<T> value) => value.IsTimeouted ? null : value.Value;
}
however, the last one won't work.
the way it is now, because of the explicit operator, it says it's double and it won't accept to return null. it doesn't seem to realize, that it's supposed to be nullable.
but if i make it Nullable<T>
, i can only put structs in it which defies the purpose of my struct. also i can't return value.Value then.
anyone ideas how i can make this work?