Unity-Technologies / Unity-Technologies/Unity.Mathematics

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

Open
#177 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
C#
Stars
1.4k
Forks
159
PR merge metrics
No merged PRs in 30d

Description

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;

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by locating the VectorX and floatX type definitions and their existing conversion and operator implementations. Reproduce the Input.mousePosition and float3 example, then verify that the relevant arithmetic and comparison expressions resolve without ambiguity and produce the intended Unity.Mathematics result type.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp, unity
Domain
computer-graphics
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.