BREAKING CHANGE: chore: rename Maybe.Just to Maybe.Create
This was a bad AI suggestion and I regret the decision.
This commit is contained in:
parent
68f527ec99
commit
479b30b60a
|
@ -27,7 +27,7 @@ public class EnumerableExtensionTests
|
||||||
var baseArray = new[] { 0, 1, 2 };
|
var baseArray = new[] { 0, 1, 2 };
|
||||||
|
|
||||||
var result = baseArray
|
var result = baseArray
|
||||||
.ConditionalAppend(Maybe.Just(3))
|
.ConditionalAppend(Maybe.Create(3))
|
||||||
.ToArray();
|
.ToArray();
|
||||||
|
|
||||||
Assert.That(result, Has.Length.EqualTo(4));
|
Assert.That(result, Has.Length.EqualTo(4));
|
||||||
|
|
|
@ -12,21 +12,21 @@ public class MaybeExtensionTests
|
||||||
public void EnumerableFirstOrEmptyTests()
|
public void EnumerableFirstOrEmptyTests()
|
||||||
{
|
{
|
||||||
Assert.AreEqual(Maybe.Empty<string>(), Array.Empty<string>().FirstOrEmpty());
|
Assert.AreEqual(Maybe.Empty<string>(), Array.Empty<string>().FirstOrEmpty());
|
||||||
Assert.AreEqual(Maybe.Just<string>("banana"), new[] { "banana" }.FirstOrEmpty());
|
Assert.AreEqual(Maybe.Create<string>("banana"), new[] { "banana" }.FirstOrEmpty());
|
||||||
}
|
}
|
||||||
|
|
||||||
[Test]
|
[Test]
|
||||||
public void TestToMaybe()
|
public void TestToMaybe()
|
||||||
{
|
{
|
||||||
Assert.AreEqual(Maybe.Empty<int>(), Maybe.Empty<int>());
|
Assert.AreEqual(Maybe.Empty<int>(), Maybe.Empty<int>());
|
||||||
Assert.AreNotEqual(Maybe.Just(293921), Maybe.Just(2));
|
Assert.AreNotEqual(Maybe.Create(293921), Maybe.Create(2));
|
||||||
}
|
}
|
||||||
|
|
||||||
[Test]
|
[Test]
|
||||||
public void MaybeNullableTests()
|
public void MaybeNullableTests()
|
||||||
{
|
{
|
||||||
Assert.IsNull(Maybe.Empty<int>().ToNullable());
|
Assert.IsNull(Maybe.Empty<int>().ToNullable());
|
||||||
Assert.IsNull(Maybe.Just(0, hasValue: _ => false).ToNullable());
|
Assert.IsNull(Maybe.Create(0, hasValue: _ => false).ToNullable());
|
||||||
}
|
}
|
||||||
|
|
||||||
[Test]
|
[Test]
|
||||||
|
|
|
@ -10,7 +10,7 @@ public class MaybeTests
|
||||||
{
|
{
|
||||||
var emptyMaybe = Maybe.Empty<int>();
|
var emptyMaybe = Maybe.Empty<int>();
|
||||||
|
|
||||||
var result = emptyMaybe.Bind(x => Maybe.Just(x + 1));
|
var result = emptyMaybe.Bind(x => Maybe.Create(x + 1));
|
||||||
|
|
||||||
Assert.That(result, Is.InstanceOf<Maybe<int>>());
|
Assert.That(result, Is.InstanceOf<Maybe<int>>());
|
||||||
Assert.That(result.HasValue, Is.False);
|
Assert.That(result.HasValue, Is.False);
|
||||||
|
@ -19,9 +19,9 @@ public class MaybeTests
|
||||||
[Test]
|
[Test]
|
||||||
public void TestBind_BindsWhenHasValue()
|
public void TestBind_BindsWhenHasValue()
|
||||||
{
|
{
|
||||||
var emptyMaybe = Maybe.Just<int>(1);
|
var emptyMaybe = Maybe.Create<int>(1);
|
||||||
|
|
||||||
var result = emptyMaybe.Bind(x => Maybe.Just(x + 1));
|
var result = emptyMaybe.Bind(x => Maybe.Create(x + 1));
|
||||||
|
|
||||||
Assert.That(result, Is.InstanceOf<Maybe<int>>());
|
Assert.That(result, Is.InstanceOf<Maybe<int>>());
|
||||||
Assert.That(result.HasValue, Is.True);
|
Assert.That(result.HasValue, Is.True);
|
||||||
|
@ -31,11 +31,11 @@ public class MaybeTests
|
||||||
[Test]
|
[Test]
|
||||||
public void TestBind_BindsWhenHasValue_NewOutputType()
|
public void TestBind_BindsWhenHasValue_NewOutputType()
|
||||||
{
|
{
|
||||||
var maybe = Maybe.Just(25);
|
var maybe = Maybe.Create(25);
|
||||||
|
|
||||||
var result = maybe.Bind(x => x switch
|
var result = maybe.Bind(x => x switch
|
||||||
{
|
{
|
||||||
25 => Maybe.Just("twenty-five"),
|
25 => Maybe.Create("twenty-five"),
|
||||||
_ => Maybe.Empty<string>()
|
_ => Maybe.Empty<string>()
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -46,9 +46,9 @@ public class MaybeTests
|
||||||
[Test]
|
[Test]
|
||||||
public void EmptyBind_DoesNotBindFactory_WhenHasValue()
|
public void EmptyBind_DoesNotBindFactory_WhenHasValue()
|
||||||
{
|
{
|
||||||
var maybe = Maybe.Just(25);
|
var maybe = Maybe.Create(25);
|
||||||
|
|
||||||
var result = maybe.EmptyBind(() => Maybe.Just(30));
|
var result = maybe.EmptyBind(() => Maybe.Create(30));
|
||||||
|
|
||||||
Assert.That(result, Is.InstanceOf<Maybe<int>>());
|
Assert.That(result, Is.InstanceOf<Maybe<int>>());
|
||||||
Assert.That(result.HasValue, Is.True);
|
Assert.That(result.HasValue, Is.True);
|
||||||
|
@ -60,7 +60,7 @@ public class MaybeTests
|
||||||
{
|
{
|
||||||
var maybe = Maybe.Empty<int>();
|
var maybe = Maybe.Empty<int>();
|
||||||
|
|
||||||
var result = maybe.EmptyBind(() => Maybe.Just(30));
|
var result = maybe.EmptyBind(() => Maybe.Create(30));
|
||||||
|
|
||||||
Assert.That(result, Is.InstanceOf<Maybe<int>>());
|
Assert.That(result, Is.InstanceOf<Maybe<int>>());
|
||||||
Assert.That(result.HasValue, Is.True);
|
Assert.That(result.HasValue, Is.True);
|
||||||
|
@ -70,9 +70,9 @@ public class MaybeTests
|
||||||
[Test]
|
[Test]
|
||||||
public void EmptyBind_DoesNotBindMaybe_WhenHasValue()
|
public void EmptyBind_DoesNotBindMaybe_WhenHasValue()
|
||||||
{
|
{
|
||||||
var maybe = Maybe.Just(25);
|
var maybe = Maybe.Create(25);
|
||||||
|
|
||||||
var result = maybe.EmptyBind(Maybe.Just(30));
|
var result = maybe.EmptyBind(Maybe.Create(30));
|
||||||
|
|
||||||
Assert.That(result, Is.InstanceOf<Maybe<int>>());
|
Assert.That(result, Is.InstanceOf<Maybe<int>>());
|
||||||
Assert.That(result.HasValue, Is.True);
|
Assert.That(result.HasValue, Is.True);
|
||||||
|
@ -84,7 +84,7 @@ public class MaybeTests
|
||||||
{
|
{
|
||||||
var maybe = Maybe.Empty<int>();
|
var maybe = Maybe.Empty<int>();
|
||||||
|
|
||||||
var result = maybe.EmptyBind(Maybe.Just(30));
|
var result = maybe.EmptyBind(Maybe.Create(30));
|
||||||
|
|
||||||
Assert.That(result, Is.InstanceOf<Maybe<int>>());
|
Assert.That(result, Is.InstanceOf<Maybe<int>>());
|
||||||
Assert.That(result.HasValue, Is.True);
|
Assert.That(result.HasValue, Is.True);
|
||||||
|
@ -109,7 +109,7 @@ public class MaybeTests
|
||||||
public void Cast_ToNullableReferenceType_ReturnsMaybe_WhenHasValue()
|
public void Cast_ToNullableReferenceType_ReturnsMaybe_WhenHasValue()
|
||||||
{
|
{
|
||||||
// Arrange
|
// Arrange
|
||||||
var maybe = Maybe.Just("banana");
|
var maybe = Maybe.Create("banana");
|
||||||
|
|
||||||
// Act
|
// Act
|
||||||
var result = maybe.Cast<string?>();
|
var result = maybe.Cast<string?>();
|
||||||
|
@ -138,7 +138,7 @@ public class MaybeTests
|
||||||
public void Cast_ToNullableValueType_ReturnsMaybe_WhenHasValue()
|
public void Cast_ToNullableValueType_ReturnsMaybe_WhenHasValue()
|
||||||
{
|
{
|
||||||
// Arrange
|
// Arrange
|
||||||
var maybe = Maybe.Just(25);
|
var maybe = Maybe.Create(25);
|
||||||
|
|
||||||
// Act
|
// Act
|
||||||
var result = maybe.Cast<int?>();
|
var result = maybe.Cast<int?>();
|
||||||
|
|
|
@ -12,7 +12,7 @@ public class NullableExtensionTests
|
||||||
|
|
||||||
[Test]
|
[Test]
|
||||||
public void TestNullableStructOfValueToMaybe() =>
|
public void TestNullableStructOfValueToMaybe() =>
|
||||||
Assert.AreEqual(Maybe.Just(25), ((int?)25).ToMaybe());
|
Assert.AreEqual(Maybe.Create(25), ((int?)25).ToMaybe());
|
||||||
|
|
||||||
[Test]
|
[Test]
|
||||||
public void TestNullableReferenceOfNullAsMaybe()
|
public void TestNullableReferenceOfNullAsMaybe()
|
||||||
|
|
|
@ -4,8 +4,8 @@ using System;
|
||||||
|
|
||||||
public static class Maybe
|
public static class Maybe
|
||||||
{
|
{
|
||||||
public static Maybe<T> Just<T>(T value) => new(value, hasValue: true);
|
public static Maybe<T> Create<T>(T value) => new(value, hasValue: true);
|
||||||
public static Maybe<T> Just<T>(T value, Func<T, bool> hasValue) => new(value, hasValue: hasValue(value));
|
public static Maybe<T> Create<T>(T value, Func<T, bool> hasValue) => new(value, hasValue: hasValue(value));
|
||||||
public static Maybe<T> Empty<T>() => new(val: default!, hasValue: false);
|
public static Maybe<T> Empty<T>() => new(val: default!, hasValue: false);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -52,7 +52,7 @@ public readonly record struct Maybe<T>
|
||||||
HasValue ? Value : throw exFactory();
|
HasValue ? Value : throw exFactory();
|
||||||
|
|
||||||
public Maybe<TOutput> Map<TOutput>(Func<T, TOutput> transformer) =>
|
public Maybe<TOutput> Map<TOutput>(Func<T, TOutput> transformer) =>
|
||||||
HasValue ? Maybe.Just(transformer(Value)) : Maybe.Empty<TOutput>();
|
HasValue ? Maybe.Create(transformer(Value)) : Maybe.Empty<TOutput>();
|
||||||
|
|
||||||
public Maybe<TOutput> Bind<TOutput>(Func<T, Maybe<TOutput>> binder) =>
|
public Maybe<TOutput> Bind<TOutput>(Func<T, Maybe<TOutput>> binder) =>
|
||||||
HasValue ? binder(Value) : Maybe.Empty<TOutput>();
|
HasValue ? binder(Value) : Maybe.Empty<TOutput>();
|
||||||
|
@ -77,7 +77,7 @@ public readonly record struct Maybe<T>
|
||||||
? default
|
? default
|
||||||
: (TOutput)Convert.ChangeType(Value, t);
|
: (TOutput)Convert.ChangeType(Value, t);
|
||||||
|
|
||||||
return Maybe.Just(output, hasValue: _ => output != null)!;
|
return Maybe.Create(output, hasValue: _ => output != null)!;
|
||||||
}
|
}
|
||||||
catch (InvalidCastException)
|
catch (InvalidCastException)
|
||||||
{
|
{
|
||||||
|
@ -85,7 +85,7 @@ public readonly record struct Maybe<T>
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static implicit operator Maybe<T>(T val) => Maybe.Just(val);
|
public static implicit operator Maybe<T>(T val) => Maybe.Create(val);
|
||||||
|
|
||||||
public static explicit operator T(Maybe<T> val) => val.Value;
|
public static explicit operator T(Maybe<T> val) => val.Value;
|
||||||
|
|
||||||
|
|
|
@ -23,10 +23,10 @@ public static class MaybeExtensions
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Maybe<TValue> TryGetValue<TKey, TValue>(this IDictionary<TKey, TValue> dict, TKey key) =>
|
public static Maybe<TValue> TryGetValue<TKey, TValue>(this IDictionary<TKey, TValue> dict, TKey key) =>
|
||||||
dict.TryGetValue(key, out var value) ? Maybe.Just(value) : Maybe.Empty<TValue>();
|
dict.TryGetValue(key, out var value) ? Maybe.Create(value) : Maybe.Empty<TValue>();
|
||||||
|
|
||||||
public static Maybe<TValue> TryGet<TKey, TValue>(this IReadOnlyDictionary<TKey, TValue> dict, TKey key) =>
|
public static Maybe<TValue> TryGet<TKey, TValue>(this IReadOnlyDictionary<TKey, TValue> dict, TKey key) =>
|
||||||
dict.TryGetValue(key, out var value) ? Maybe.Just(value) : Maybe.Empty<TValue>();
|
dict.TryGetValue(key, out var value) ? Maybe.Create(value) : Maybe.Empty<TValue>();
|
||||||
|
|
||||||
public static T? ToNullable<T>(this Maybe<T> item) where T : struct =>
|
public static T? ToNullable<T>(this Maybe<T> item) where T : struct =>
|
||||||
item.HasValue ? item.Value : null;
|
item.HasValue ? item.Value : null;
|
||||||
|
|
|
@ -7,7 +7,7 @@ module Array =
|
||||||
|
|
||||||
module Option =
|
module Option =
|
||||||
let toMaybe : 'a option -> Maybe<'a> = function
|
let toMaybe : 'a option -> Maybe<'a> = function
|
||||||
| Some x -> x |> Maybe.Just
|
| Some x -> x |> Maybe.Create
|
||||||
| None -> Maybe.Empty ()
|
| None -> Maybe.Empty ()
|
||||||
|
|
||||||
let fromMaybe : Maybe<'a> -> 'a option = function
|
let fromMaybe : Maybe<'a> -> 'a option = function
|
||||||
|
|
Loading…
Reference in New Issue
Block a user