TensorFlow.NET icon indicating copy to clipboard operation
TensorFlow.NET copied to clipboard

Checking if NDArray is null returns wrong results

Open Superpiffer opened this issue 3 years ago • 1 comments

If I try to check if an NDArray is null, I get strange behavior:

NDArray n = null;
bool b1 = n is null;  // TRUE
bool b2 = n == null;  // FALSE!

I saw in NDArray.Operators.cs that if the second value is null, the result is false: https://github.com/SciSharp/TensorFlow.NET/blob/ce3ddb2321312894ef4355b2809802f7d6f62e1f/src/TensorFlowNET.Core/NumPy/NDArray.Operators.cs#L28-L29

Is that a wanted behavior? I think that if both values are null, the result should be true.

Superpiffer avatar May 03 '22 07:05 Superpiffer

Just got into this after migrating from the older version that used NumSharp. Almost had a heart attack by watching an expression if (x != null) throwing a NullReferenceException. It looks like it was an intended behavior, according to the implementation, but wait, this is not a Python anymore! Despite there is an "is" operator in C# now, this is not a common idiomatic form. No one should expect from "x == null" anything else except checking for null! In other words, let's not try to turn C# into a Python, please?

m0nzderr avatar Aug 05 '22 22:08 m0nzderr

Solved https://github.com/SciSharp/TensorFlow.NET/commit/5821275145e5123d1acbc4094acd2baef06ac138

Superpiffer avatar Feb 20 '23 10:02 Superpiffer