Introduction:
I have a few classes which do the same work, but with different value types (e.g. Vectors of floats or integers).
Now I want to be able to check for equality, this equality should also work in between the types (such as vectorF == vectorI).
Also, it should be possible to do a null check (vectorF == null).
Approach:
My approach is to create multiple overloads for the == and != operators, one for each possible combination.
public sealed class VectorF
{
[...]
public static bool operator == (VectorF left, VectorI right)
{
// Implementation...
}
public static bool operator == (VectorF left, VectorF right)
{
// Implementation...
}
// Same for != operator
[...]
}
Problem:
Using multiple overloads, I cannot just do a null check with the == operator, as the call would be ambiguous.
var v = new VectorF([...]);
if (v == null) // This call is ambiguous
[...]
I'm aware of the possibility to use a ReferenceEquals or null casting instead, but that approachis a serious restriction for me.
var v = new VectorF([...]);
if(object.ReferenceEquals(v, null)) // Would work, is not user friendly.
[...]
if(v == (VectorF)null) // Would also work, is neither user friendly.
[...]
Question:
Is there a way to implement the == operator in a way, that it allows the simple null check, and allows for equality check between the different vectors?
Alternatively, is there another way how I could/should implement this?
C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...
C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.
What is C? C is a general-purpose programming language created by Dennis Ritchie at the Bell Laboratories in 1972. It is a very popular language, despite being old. C is strongly associated with UNIX, as it was developed to write the UNIX operating system.
In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.
I'd push back on the whole design to begin with. I would never implement ==
with value semantics between different types, I'd find it rather confusing: instaceTypedA == instanceTypedB
yells reference equality (at least to me).
If you need this to work, then implement an implicit conversion between VectorI
and VectorF
. This is how the framework works. When you do the following:
int i = 1;
double d = 1;
var b = i == d;
An oveload ==(int, double)
isn't magically produced. What happens is that i
is implicitly converted to double
and ==(double, double)
is invoked.
You can turn around the comparison by using is
:
if (v is VectorF)
This check will fail if v
is null
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With