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:
Kyle Ratti 2024-04-30 13:18:51 -04:00
parent 68f527ec99
commit 479b30b60a
No known key found for this signature in database
GPG Key ID: 4D429B6287C68DD9
7 changed files with 26 additions and 26 deletions

View File

@ -27,7 +27,7 @@ public class EnumerableExtensionTests
var baseArray = new[] { 0, 1, 2 };
var result = baseArray
.ConditionalAppend(Maybe.Just(3))
.ConditionalAppend(Maybe.Create(3))
.ToArray();
Assert.That(result, Has.Length.EqualTo(4));

View File

@ -12,21 +12,21 @@ public class MaybeExtensionTests
public void EnumerableFirstOrEmptyTests()
{
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]
public void TestToMaybe()
{
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]
public void MaybeNullableTests()
{
Assert.IsNull(Maybe.Empty<int>().ToNullable());
Assert.IsNull(Maybe.Just(0, hasValue: _ => false).ToNullable());
Assert.IsNull(Maybe.Create(0, hasValue: _ => false).ToNullable());
}
[Test]

View File

@ -10,7 +10,7 @@ public class MaybeTests
{
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.HasValue, Is.False);
@ -19,9 +19,9 @@ public class MaybeTests
[Test]
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.HasValue, Is.True);
@ -31,11 +31,11 @@ public class MaybeTests
[Test]
public void TestBind_BindsWhenHasValue_NewOutputType()
{
var maybe = Maybe.Just(25);
var maybe = Maybe.Create(25);
var result = maybe.Bind(x => x switch
{
25 => Maybe.Just("twenty-five"),
25 => Maybe.Create("twenty-five"),
_ => Maybe.Empty<string>()
});
@ -46,9 +46,9 @@ public class MaybeTests
[Test]
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.HasValue, Is.True);
@ -60,7 +60,7 @@ public class MaybeTests
{
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.HasValue, Is.True);
@ -70,9 +70,9 @@ public class MaybeTests
[Test]
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.HasValue, Is.True);
@ -84,7 +84,7 @@ public class MaybeTests
{
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.HasValue, Is.True);
@ -109,7 +109,7 @@ public class MaybeTests
public void Cast_ToNullableReferenceType_ReturnsMaybe_WhenHasValue()
{
// Arrange
var maybe = Maybe.Just("banana");
var maybe = Maybe.Create("banana");
// Act
var result = maybe.Cast<string?>();
@ -138,7 +138,7 @@ public class MaybeTests
public void Cast_ToNullableValueType_ReturnsMaybe_WhenHasValue()
{
// Arrange
var maybe = Maybe.Just(25);
var maybe = Maybe.Create(25);
// Act
var result = maybe.Cast<int?>();

View File

@ -12,7 +12,7 @@ public class NullableExtensionTests
[Test]
public void TestNullableStructOfValueToMaybe() =>
Assert.AreEqual(Maybe.Just(25), ((int?)25).ToMaybe());
Assert.AreEqual(Maybe.Create(25), ((int?)25).ToMaybe());
[Test]
public void TestNullableReferenceOfNullAsMaybe()

View File

@ -4,8 +4,8 @@ using System;
public static class Maybe
{
public static Maybe<T> Just<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) => new(value, hasValue: true);
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);
}
@ -52,7 +52,7 @@ public readonly record struct Maybe<T>
HasValue ? Value : throw exFactory();
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) =>
HasValue ? binder(Value) : Maybe.Empty<TOutput>();
@ -77,7 +77,7 @@ public readonly record struct Maybe<T>
? default
: (TOutput)Convert.ChangeType(Value, t);
return Maybe.Just(output, hasValue: _ => output != null)!;
return Maybe.Create(output, hasValue: _ => output != null)!;
}
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;

View File

@ -23,10 +23,10 @@ public static class MaybeExtensions
}
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) =>
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 =>
item.HasValue ? item.Value : null;

View File

@ -7,7 +7,7 @@ module Array =
module Option =
let toMaybe : 'a option -> Maybe<'a> = function
| Some x -> x |> Maybe.Just
| Some x -> x |> Maybe.Create
| None -> Maybe.Empty ()
let fromMaybe : Maybe<'a> -> 'a option = function