// Copyright (c) ppy Pty Ltd . Licensed under the MIT Licence. // See the LICENCE file in the repository root for full licence text. #nullable enable namespace osu.Game.Utils { /// /// A wrapper over a value and a boolean denoting whether the value is valid. /// /// The type of value stored. public readonly ref struct Optional { /// /// The stored value. /// public readonly T Value; /// /// Whether is valid. /// /// /// If is a reference type, null may be valid for . /// public readonly bool HasValue; private Optional(T value) { Value = value; HasValue = true; } /// /// Returns if it's valid, or a given fallback value otherwise. /// /// /// Shortcase for: optional.HasValue ? optional.Value : fallback. /// /// The fallback value to return if is false. /// public T GetOr(T fallback) => HasValue ? Value : fallback; public static implicit operator Optional(T value) => new Optional(value); } }