Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Operand ambiguity occurs when referencing built-in Vector types via arithmetic and comparison operators #177

Open
jstine35 opened this issue Oct 11, 2020 · 0 comments

Comments

@jstine35
Copy link

Given the following code snip:

static float3 lastMouseSpinViewPos;
var delta = Input.mousePosition - lastMouseSpinViewPos;

The following compiler error occurs:

Operator '-' is ambiguous on operands of type 'Vector3' and 'float3'

The reason is that the compiler doesn't know if it should implicitly convert the first operand to float3, or implicitly convert the second operand to Vector3.

Expected Behavior

My expectation is that Unity.Mathematics types should assume priority in this case: the first operand is converted to float3 and the result type is a float3. I'm not sure if there's a valid case for expecting behavior favoring Vector3, and if there were, it could be achieved by using explicit (Vector3) cast or type definition. Seeking a legacy Vector3 result is an edge case and is well-suited to be delegated to an explicit cast condition.

Cause of Problem

  • Input.mousePosition is a unity built-in property or field, so its type is Vector3
  • There are no explicit arithmetic operator implementations for VectorX types and floatX types.

Proposed solution

For the sake of streamlining interoperability with Unity built-in class Vector types, I think it makes sense to provide explicit implementations of arithmetic and comparison operators, like so:

        public static float3 operator +(Vector3 l, float3 r)  { return (float3)l + r; }
        public static float3 operator -(Vector3 l, float3 r)  { return (float3)l - r; }

        public static float3 operator +(float3 l, Vector3 r)  { return l + (float3)r; }
        public static float3 operator -(float3 l, Vector3 r)  { return l - (float3)r; }

Workaround

Add explicit typecast in all situations, like so:

static float3 lastMouseSpinViewPos;
var delta = (float3)Input.mousePosition - lastMouseSpinViewPos;
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

No branches or pull requests

1 participant