Skip to content

feature/optional-nullable-structs #60

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions OnixLabs.Core.UnitTests/OptionalTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,38 @@ public void OptionalOfShouldProduceExpectedResultForExplicitNonDefaultValues()
Assert.Equal("abc", text);
}

[Fact(DisplayName = "Optional.Of should produce the expected result for null nullable struct values.")]
public void OptionalOfShouldProduceExpectedResultForNullNullableStructValues()
{
// Given / When
Optional<int> number = Optional<int>.Of((int?)null);
Optional<Guid> identifier = Optional<Guid>.Of((Guid?)default);

// Then
Assert.False(number.HasValue);
Assert.IsType<None<int>>(number);

Assert.False(identifier.HasValue);
Assert.IsType<None<Guid>>(identifier);
}

[Fact(DisplayName = "Optional.Of should produce the expected result for non-null nullable struct values.")]
public void OptionalOfShouldProduceExpectedResultForNonNullNullableStructValues()
{
// Given / When
Optional<int> number = Optional<int>.Of((int?)123);
Optional<Guid> identifier = Optional<Guid>.Of((Guid?)Guid.Empty);

// Then
Assert.True(number.HasValue);
Assert.IsType<Some<int>>(number);
Assert.Equal(123, number);

Assert.True(identifier.HasValue);
Assert.IsType<Some<Guid>>(identifier);
Assert.Equal(Guid.Empty, identifier);
}

[Fact(DisplayName = "Optional.Some should produce the expected result.")]
public void OptionalSomeShouldProduceExpectedResult()
{
Expand Down
2 changes: 1 addition & 1 deletion OnixLabs.Core/OnixLabs.Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<NeutralLanguage>en</NeutralLanguage>
<Copyright>Copyright © ONIXLabs 2020</Copyright>
<RepositoryUrl>https://github.com/onix-labs/onixlabs-dotnet</RepositoryUrl>
<PackageVersion>8.5.0</PackageVersion>
<PackageVersion>8.6.0</PackageVersion>
<PackageLicenseUrl></PackageLicenseUrl>
</PropertyGroup>
<PropertyGroup>
Expand Down
17 changes: 16 additions & 1 deletion OnixLabs.Core/Optional.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,22 @@ internal Optional()
/// Returns a new instance of the <see cref="Optional{T}"/> class, where the underlying value is present if
/// the specified value is not <see langword="default"/>; otherwise, the underlying value is <see cref="None"/>.
/// </returns>
public static Optional<T> Of(T? value) => value is null || EqualityComparer<T>.Default.Equals(value, default) ? None : Some(value);
public static Optional<T> Of(T? value) => value is not null && !EqualityComparer<T>.Default.Equals(value, default)
? Some(value)
: None;

/// <summary>
/// Creates a new instance of the <see cref="Optional{T}"/> class, where the underlying value is present if
/// the specified value is not <see langword="null"/>; otherwise, the underlying value is <see cref="None"/>.
/// </summary>
/// <param name="value">The underlying optional value.</param>
/// <returns>
/// Returns a new instance of the <see cref="Optional{T}"/> class, where the underlying value is present if
/// the specified value is not <see langword="null"/>; otherwise, the underlying value is <see cref="None"/>.
/// </returns>
public static Optional<TStruct> Of<TStruct>(TStruct? value) where TStruct : struct => value.HasValue
? Optional<TStruct>.Some(value.Value)
: Optional<TStruct>.None;

/// <summary>
/// Creates a new instance of the <see cref="Optional{T}"/> class, where the underlying value is present if
Expand Down
2 changes: 1 addition & 1 deletion OnixLabs.Numerics/OnixLabs.Numerics.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<Copyright>Copyright © ONIXLabs 2020</Copyright>
<RepositoryUrl>https://github.com/onix-labs/onixlabs-dotnet</RepositoryUrl>
<PackageVersion>8.5.0</PackageVersion>
<PackageVersion>8.6.0</PackageVersion>
<LangVersion>12</LangVersion>
<PackageLicenseUrl></PackageLicenseUrl>
</PropertyGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<Copyright>Copyright © ONIXLabs 2020</Copyright>
<RepositoryUrl>https://github.com/onix-labs/onixlabs-dotnet</RepositoryUrl>
<PackageVersion>8.5.0</PackageVersion>
<PackageVersion>8.6.0</PackageVersion>
<LangVersion>12</LangVersion>
<PackageLicenseUrl></PackageLicenseUrl>
</PropertyGroup>
Expand Down
Loading