Skip to content

Conversation

zbynek001
Copy link
Contributor

fixes #3564

@@ -144,7 +144,7 @@ public static string BetweenDoubleQuotes(this string self)
/// <returns>TBD</returns>
public static T GetOrElse<T>(this T obj, T elseValue)
{
if (obj.Equals(default(T)))
if (obj == null || obj.Equals(default(T)))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we should just remove that method and use ?? instead? Btw. this could cause strange behavior i.e. elseValue on 0.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what kind of strange behavior you have on mind? With structs the first condition will never match since they cannot be null, so this will only change behavior for classes

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Given obj = 0:

if (obj == null || obj.Equals(default(T)))
if (0 == null || 0.Equals(default(int)))
if (false || 0.Equals(0))
if (false || true)
if (true)

What I mean here is that this method has no sense in context of value types, unlike null 0 is not an absence of value. This method was ported straightforward from scala for null checks way back in the past (before elvis operator existed AFAIK).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

right, I thought you were referencing to the change and you were talking about the method itself.
It's used only on few places so i can remove it completely and change the code where it's used if you want

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That would be great! 👍 Thanks for the patience.

@Aaronontheweb Aaronontheweb merged commit 810312c into akkadotnet:dev Sep 5, 2018
@zbynek001 zbynek001 deleted the GetOrElse-fix branch July 16, 2021 12:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

GetOrElse NullReferenceException
3 participants