Skip to content

Commit

Permalink
Use _value field where possible.
Browse files Browse the repository at this point in the history
Allows more code to be inlined by not checking `HasValue` twice.
  • Loading branch information
grokys committed Dec 4, 2019
1 parent b140310 commit 8e9a1cc
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 10 deletions.
10 changes: 5 additions & 5 deletions src/Avalonia.Base/Data/BindingValue.cs
Original file line number Diff line number Diff line change
Expand Up @@ -135,10 +135,10 @@ private BindingValue(BindingValueType type, T value, Exception? error)
/// Converts the binding value to an <see cref="Optional{T}"/>.
/// </summary>
/// <returns></returns>
public Optional<T> ToOptional() => HasValue ? new Optional<T>(Value) : default;
public Optional<T> ToOptional() => HasValue ? new Optional<T>(_value) : default;

/// <inheritdoc/>
public override string ToString() => HasError ? $"Error: {Error!.Message}" : Value?.ToString() ?? "(null)";
public override string ToString() => HasError ? $"Error: {Error!.Message}" : _value?.ToString() ?? "(null)";

/// <summary>
/// Converts the value to untyped representation, using <see cref="AvaloniaProperty.UnsetValue"/>,
Expand All @@ -152,7 +152,7 @@ private BindingValue(BindingValueType type, T value, Exception? error)
{
BindingValueType.UnsetValue => AvaloniaProperty.UnsetValue,
BindingValueType.DoNothing => BindingOperations.DoNothing,
BindingValueType.Value => Value,
BindingValueType.Value => _value,
BindingValueType.BindingError =>
new BindingNotification(Error, BindingErrorType.Error),
BindingValueType.BindingErrorWithFallback =>
Expand Down Expand Up @@ -190,7 +190,7 @@ public BindingValue<T> WithValue(T value)
/// </summary>
/// <param name="defaultValue">The default value.</param>
/// <returns>The value.</returns>
public T ValueOrDefault(T defaultValue = default) => HasValue ? Value : defaultValue;
public T ValueOrDefault(T defaultValue = default) => HasValue ? _value : defaultValue;

/// <summary>
/// Gets the value of the binding value if present, otherwise a default value.
Expand All @@ -204,7 +204,7 @@ public BindingValue<T> WithValue(T value)
public TResult ValueOrDefault<TResult>(TResult defaultValue = default)
{
return HasValue ?
Value is TResult result ? result : default
_value is TResult result ? result : default
: defaultValue;
}

Expand Down
10 changes: 5 additions & 5 deletions src/Avalonia.Base/Data/Optional.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,23 +54,23 @@ public Optional(T value)
public bool Equals(Optional<T> other) => this == other;

/// <inheritdoc/>
public override int GetHashCode() => HasValue ? Value!.GetHashCode() : 0;
public override int GetHashCode() => HasValue ? _value?.GetHashCode() ?? 0 : 0;

/// <summary>
/// Casts the value (if any) to an <see cref="object"/>.
/// </summary>
/// <returns>The cast optional value.</returns>
public Optional<object> ToObject() => HasValue ? new Optional<object>(Value) : default;
public Optional<object> ToObject() => HasValue ? new Optional<object>(_value) : default;

/// <inheritdoc/>
public override string ToString() => HasValue ? Value?.ToString() ?? "(null)" : "(empty)";
public override string ToString() => HasValue ? _value?.ToString() ?? "(null)" : "(empty)";

/// <summary>
/// Gets the value if present, otherwise a default value.
/// </summary>
/// <param name="defaultValue">The default value.</param>
/// <returns>The value.</returns>
public T ValueOrDefault(T defaultValue = default) => HasValue ? Value : defaultValue;
public T ValueOrDefault(T defaultValue = default) => HasValue ? _value : defaultValue;

/// <summary>
/// Gets the value if present, otherwise a default value.
Expand All @@ -84,7 +84,7 @@ public Optional(T value)
public TResult ValueOrDefault<TResult>(TResult defaultValue = default)
{
return HasValue ?
Value is TResult result ? result : default
_value is TResult result ? result : default
: defaultValue;
}

Expand Down

0 comments on commit 8e9a1cc

Please sign in to comment.