Browse Source

Cleaned up Optional

tags/1.0-rc
RogueException 9 years ago
parent
commit
7f3b886479
2 changed files with 32 additions and 10 deletions
  1. +1
    -1
      src/Discord.Net/API/DiscordRawClient.cs
  2. +31
    -9
      src/Discord.Net/API/Optional.cs

+ 1
- 1
src/Discord.Net/API/DiscordRawClient.cs View File

@@ -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;


+ 31
- 9
src/Discord.Net/API/Optional.cs View File

@@ -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);

/// <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 bool Equals(object other)
{
if (!IsSpecified) return other == null;
if (other == null) return false;
return _value.Equals(other);
}

public override string ToString() => IsSpecified ? (Value?.ToString() ?? null) : null;
public override int GetHashCode() => IsSpecified ? _value.GetHashCode() : 0;
public override string ToString() => IsSpecified ? _value.ToString() : "";

public static implicit operator Optional<T>(T value) => new Optional<T>(value);
public static implicit operator T(Optional<T> value) => value.Value;
}
}

Loading…
Cancel
Save