Overload equality operator ("=") on Vector2 in F # - different vectors are equal
Using math operators works with XNA Vector2s, thanks to overloads such as:
new Vector2(1.0f, 2.0f) + new Vector2(2.0f, 3.0f) // {X:3 Y:5}
(very comfortably)
But look at this:
// Okay, this works fine.
new Vector2(1.0f, 2.0f) = new Vector2(1.0f, 2.0f) // true
// Is it checking for type equality?
new Vector2(1.0f, 2.0f) = new Vector2(6.0f, 5.0f) // true
So why doesn't it call overloading on Vector2 for op_Equality
(same deal for op_Inequality
)? They work as expected when called directly.
PS: If it matters, I suppose it is not, I am running this under Mono and Monogame.
PPS: This is mostly just annoying that I can't use =
, but I can just use Vector2.Equals if I really need to.
source to share
It looks like there must be some difference between the different versions of the MonoGame compilations, because when I run the code on Windows (using the version WindowsGL
) in F # Interactive, I get the expected output:
> #r @"C:\Program Files (x86)\MonoGame\v3.0\Assemblies\WindowsGL\MonoGame.Framework.dll"
open Microsoft.Xna.Framework;;
> new Vector2(1.0f, 2.0f) = new Vector2(1.0f, 2.0f);;
val it : bool = true
> new Vector2(1.0f, 2.0f) = new Vector2(6.0f, 5.0f);;
val it : bool = false
I also tried referring to the assemblies in the directory Linux
(although I did it on Windows) and it works too, so something strange is happening. What platform are you using? Can you try running just the above script in F # interactive to see if it works outside of your application?
source to share
The F # equality operator cannot be overridden, but for non-F # types it must be equivalent to using a non-static method Equals
(as described in pseudocode in the spec ). Perhaps there is something else going on in your example?
source to share