Cleaned up Optional
This commit is contained in:
@@ -473,7 +473,7 @@ namespace Discord.API
|
||||
if (args.Limit <= 0) throw new ArgumentOutOfRangeException(nameof(args.Limit));
|
||||
if (args.Offset < 0) throw new ArgumentOutOfRangeException(nameof(args.Offset));
|
||||
|
||||
int limit = args.Limit.IsSpecified ? args.Limit.Value : int.MaxValue;
|
||||
int limit = args.Limit.GetValueOrDefault(int.MaxValue);
|
||||
int offset = args.Offset;
|
||||
|
||||
List<GuildMember[]> result;
|
||||
|
||||
@@ -1,26 +1,48 @@
|
||||
namespace Discord.API
|
||||
using System;
|
||||
|
||||
namespace Discord.API
|
||||
{
|
||||
//Based on https://github.com/dotnet/coreclr/blob/master/src/mscorlib/src/System/Nullable.cs
|
||||
public struct Optional<T> : IOptional
|
||||
{
|
||||
private readonly T _value;
|
||||
|
||||
/// <summary> Gets the value for this paramter, or default(T) if unspecified. </summary>
|
||||
public T Value { get; }
|
||||
public T Value
|
||||
{
|
||||
get
|
||||
{
|
||||
if (!IsSpecified)
|
||||
throw new InvalidOperationException("This property has no value set.");
|
||||
return _value;
|
||||
}
|
||||
}
|
||||
/// <summary> Returns true if this value has been specified. </summary>
|
||||
public bool IsSpecified { get; }
|
||||
|
||||
object IOptional.Value => Value;
|
||||
object IOptional.Value => _value;
|
||||
|
||||
/// <summary> Creates a new Parameter with the provided value. </summary>
|
||||
public Optional(T value)
|
||||
{
|
||||
Value = value;
|
||||
_value = value;
|
||||
IsSpecified = true;
|
||||
}
|
||||
|
||||
public T GetValueOrDefault() => _value;
|
||||
public T GetValueOrDefault(T defaultValue) => IsSpecified ? _value : default(T);
|
||||
|
||||
public override bool Equals(object other)
|
||||
{
|
||||
if (!IsSpecified) return other == null;
|
||||
if (other == null) return false;
|
||||
return _value.Equals(other);
|
||||
}
|
||||
|
||||
public override int GetHashCode() => IsSpecified ? _value.GetHashCode() : 0;
|
||||
public override string ToString() => IsSpecified ? _value.ToString() : "";
|
||||
|
||||
/// <summary> Implicitly creates a new Parameter from an existing value. </summary>
|
||||
public static implicit operator Optional<T>(T value) => new Optional<T>(value);
|
||||
/// <summary> Implicitly creates a new Parameter from an existing value. </summary>
|
||||
public static implicit operator T(Optional<T> param) => param.Value;
|
||||
|
||||
public override string ToString() => IsSpecified ? (Value?.ToString() ?? null) : null;
|
||||
public static implicit operator T(Optional<T> value) => value.Value;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user