Skip to content

Commit

Permalink
added factory-methods and extensions for nullables
Browse files Browse the repository at this point in the history
  • Loading branch information
duffleit committed Dec 1, 2016
1 parent b298b2d commit 8a4d685
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 1 deletion.
12 changes: 12 additions & 0 deletions fluentOptionals.Tests/OptionalTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,18 @@ public void ToOptional_WhenPredicateForNullIsGiven_ThenNoneGetsReturned()
((string) null).ToOptional(x => x.Length == 10).ShouldBeNone();
}

[Test]
public void ToOptional_WhenNullableWithoutValueIsGiven_ThenNoneGetsReturned()
{
new int?().ToOptional().ShouldBeNone();
}

[Test]
public void ToOptional_WhenNullableWithValueIsGiven_ThenSomeGetsReturned()
{
new int?(10).ToOptional().ShouldBeSome();
}

#endregion
}

Expand Down
7 changes: 6 additions & 1 deletion fluentOptionals/Optional.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,14 @@ public static Optional<T> From<T>(T value, Func<T, bool> condition)

return condition(value) ? Some(value) : None<T>();
}

public static Optional<T> From<T>(T? nullable) where T : struct
{
return nullable?.ToSome() ?? None<T>();
}
}

#endregion
#endregion

public struct Optional<T> :
IComparable<Optional<T>>,
Expand Down
5 changes: 5 additions & 0 deletions fluentOptionals/OptionalCreateExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,10 @@ public static Optional<T> ToOptional<T>(this T value, Func<T, bool> condition)
{
return Optional.From(value, condition);
}

public static Optional<T> ToOptional<T>(T? nullable) where T : struct
{
return Optional.From(nullable);
}
}
}

0 comments on commit 8a4d685

Please sign in to comment.