TryGetValue_returns_false_if_source_is_empty_and_out_parameter_is_null #482
-
Is this useful in real scenarios? Why would we allow nullable type inside a MayBe? The purpose of Maybe is to say Value or nothing. Value = int, HasValue = true I am trying to imagine where I would want to do something like var maybe = Maybe<int?>.None;
bool result = maybe.TryGetValue(out int? value); vs var maybe = Maybe<int>.None;
bool result = maybe.TryGetValue(out int value); |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
This is probably not a useful scenario in general, but still, there could be usecases where you are using Also, I don't think there's a way to restrict the use a generic type to non-nullable types only. |
Beta Was this translation helpful? Give feedback.
-
I found having T? _value within Maybe where T is int? confusing so my friend gave an implementation of maybe that will not allows nulls. For now I will keep the restriction till some real world scenario needs nullable int. public readonly struct Maybe<T> :
IEquatable<T>,
IEquatable<Maybe<T>>
where T : notnull
{ Full implementation here. I am also not sure what value the interface IMaybe adds. |
Beta Was this translation helpful? Give feedback.
This is probably not a useful scenario in general, but still, there could be usecases where you are using
Maybe<T>
without being able to control the type ofT
(which may turn intoint?
).Also, I don't think there's a way to restrict the use a generic type to non-nullable types only.
?
is a shortcut forNullable<T>
which itself is a struct.