refactor: Rename Optional members PascalCase

Also reformat code according to new style guidlines
This commit is contained in:
John Colvin committed 2026-03-08 16:00:50 +00:00
1 parent 6ea955aacd
commit e7bd57964e
6 files changed
+68 -58

No files matched your search

@@ -513,7 +513,7 @@ namespace mbts.spark {
public ItemSettings With(Optional<FontParamsProto> font = default, uint? background_color = null, bool? show_label = null, bool in_place = false) {
var rv = in_place ? this : Clone();
if (font.has_value) {
if (font.HasValue) {
rv.font = font;
}
if (background_color.HasValue) {
+3 -3
View File
@@ -19,15 +19,15 @@ namespace Spark.Core {
this.syncRoot = syncRoot;
}
public bool IsValueCreated => _value.has_value;
public bool IsValueCreated => _value.HasValue;
public T Value {
get {
// check has_value outside of lock since, once set to true, it doesn't change
if (!_value.has_value) {
if (!_value.HasValue) {
lock (syncRoot) {
// check has_value again (may have become true since first check)
if (!_value.has_value) {
if (!_value.HasValue) {
_value = _create();
}
}
+58 -48
View File
@@ -1,53 +1,63 @@
namespace Spark.Core {
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
namespace Spark.Core;
public readonly struct Optional<T> : IEquatable<Optional<T>>, IEquatable<T> {
readonly T? _value;
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
public Optional(T value) {
_value = value;
has_value = true;
}
public readonly struct Optional<T> : IEquatable<Optional<T>>, IEquatable<T> {
readonly T? value;
public static Optional<T> Empty => default;
public bool has_value { get; }
[MemberNotNull]
public T value => has_value ?
_value ?? throw new InvalidOperationException() :
throw new InvalidOperationException($"value uninitialized");
public T value_or(T default_value) =>
has_value ? value : default_value;
public bool Equals(Optional<T> other) =>
has_value == other.has_value &&
(!has_value || EqualityComparer<T>.Default.Equals(value, other.value));
public override bool Equals(object? obj) =>
obj is Optional<T> other ?
Equals(other) :
obj is T other_val && Equals(other_val);
public bool Equals(T? other) =>
has_value && EqualityComparer<T>.Default.Equals(value, other);
public override int GetHashCode() =>
has_value ? value?.GetHashCode() ?? throw new InvalidOperationException() : 0;
public override string? ToString() =>
has_value ? value?.ToString() : string.Empty;
public static implicit operator Optional<T>(T value) =>
new(value);
public static implicit operator T(Optional<T> optional) =>
optional.value;
public static bool operator ==(Optional<T> left, Optional<T> right) => left.Equals(right);
public static bool operator !=(Optional<T> left, Optional<T> right) => !(left == right);
public Optional(T value) {
this.value = value;
HasValue = true;
}
public static Optional<T> Empty => default;
[MemberNotNullWhen(true, nameof(value))]
public bool HasValue { get; }
public T Value => HasValue
? value
: throw new InvalidOperationException("Value not set");
public T GetValueOrDefault(T defaultValue) =>
HasValue
? Value
: defaultValue;
public bool Equals(Optional<T> other) =>
HasValue == other.HasValue &&
(!HasValue || EqualityComparer<T>.Default.Equals(Value, other.Value));
public bool Equals(T? other) =>
HasValue && EqualityComparer<T>.Default.Equals(Value, other);
public override bool Equals(object? obj) =>
obj is Optional<T> other
? Equals(other)
: obj is T otherVal && Equals(otherVal);
public override int GetHashCode() =>
HasValue
? Value?.GetHashCode() ?? throw new InvalidOperationException()
: 0;
public override string? ToString() =>
HasValue
? Value?.ToString()
: string.Empty;
public static implicit operator Optional<T>(T value) =>
new(value);
[return: MaybeNull]
public static implicit operator T(Optional<T> optional) =>
optional.Value;
public static bool operator ==(Optional<T> left, Optional<T> right) =>
left.Equals(right);
public static bool operator !=(Optional<T> left, Optional<T> right) =>
!(left == right);
}
+1 -1
View File
@@ -73,7 +73,7 @@ namespace mbts.spark {
quantity ?? trade_info.Quantity,
aggressorSide ?? trade_info.AggressorSide,
qty_display ?? trade_info.qty_display,
greeks_tuple.value_or(trade_info.greeks_tuple),
greeks_tuple.GetValueOrDefault(trade_info.greeks_tuple),
volume ?? trade_info.volume,
recordId ?? trade_info.RecordId);
}
@@ -88,7 +88,7 @@ namespace Spark.Trading {
new(trade);
public DayFillsRowData with(Optional<SecurityImmutable?> security, uint? user_id = null) =>
new(trade, security.value_or(this.security), user_id ?? this.user_id);
new(trade, security.GetValueOrDefault(this.security), user_id ?? this.user_id);
public bool Equals(DayFillsRowData other) =>
Equals(trade, other.trade) &&
+4 -4
View File
@@ -25,10 +25,10 @@ public static class OrderUpdateExtensions {
Optional<IReadOnlyList<FillImmutable>?> newFills = default,
OrderUpdateFlags? flags = null) =>
new(
security.has_value ? security.value : er.Security,
security.HasValue ? security.Value : er.Security,
ack ?? er.Ack,
order.has_value ? order.value : er.Order,
lastFill.has_value ? lastFill.value : er.LastFill,
newFills.has_value ? newFills.value : er.NewFills,
order.HasValue ? order.Value : er.Order,
lastFill.HasValue ? lastFill.Value : er.LastFill,
newFills.HasValue ? newFills.Value : er.NewFills,
flags ?? er.Flags);
}