Skip to content

Commit 0053717

Browse files
committed
Avoid losing of null value
1 parent 6d58634 commit 0053717

File tree

1 file changed

+5
-1
lines changed

1 file changed

+5
-1
lines changed

src/DotNext/Optional.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,11 @@ private static byte GetKindUnsafe([DisallowNull] ref T optionalValue)
271271
/// <returns>The value represented as <see cref="Optional{T}"/> or <see cref="Optional{T}.None"/> if there is no value.</returns>
272272
public static Optional<T> Create<TMonad>(TMonad value)
273273
where TMonad : struct, IOptionMonad<T>
274-
=> value.TryGet(out var result) ? new(result) : None;
274+
=> value is Optional<T>
275+
? Unsafe.BitCast<TMonad, Optional<T>>(value)
276+
: value.TryGet(out var result)
277+
? new(result)
278+
: None;
275279

276280
/// <summary>
277281
/// Determines whether the object represents meaningful value.

0 commit comments

Comments
 (0)