[Bug?] When `Maybe<T>` has a default value, the result is Maybe.HasValue == true
jeffward01 opened this issue · comments
Hello all,
I was doing some work, and I discovered this strange bug, at least i'd call it a bug. In my opinion, null
and default
are both no value. When an object is set to default
, the value is none, it just is the object itself. Thats why 0 == defaullt(int)
.
I ran into the case where I would like Guid.Empty == Maybe.None
, however that is not the case. Please see below:
[Fact]
public void MaybeShouldBeNone_GuidEmpty()
{
Maybe<Guid> shouldBeEmpty = Guid.Empty;
Assert.True(0 == default(int));
Assert.True(Guid.Empty == default);
// This fails
Assert.True(shouldBeEmpty.HasNoValue);
}
Is this by design?
I do not think it is a bug. Maybe
protects against null values, not the default values.
Let's say we have int
instead of Guid
. Maybe<int> = 0;
is a valid value, even though 0 is the default value of an int
variable.
If you need validation against the default value, You can use something like this:
var result = Result.Success(Guid.Empty)
.Ensure(guid => guid != default, "Guid is not set");
Assert.True(result.IsFailure);
@rutkowskit - Thanks for this, this makes sense. I think in my head its the distinction between null
and empty, and I for some reason was under the impression that Maybe<T>
also worked for empty
Thanks for the example also!