Perchik
Perchik

Reputation: 5620

There exists both implicit conversions from 'float' and 'float' and from 'float' to 'float'

In probably the best error message I've gotten in awhile, I'm curious as to what went wrong.

The original code

float currElbowAngle = LeftArm ? Elbow.transform.localRotation.eulerAngles.y 
                               : 360f - Elbow.transform.localRotation.eulerAngles.y

I'm using Unity3d and C#; LeftArm is a bool type and according to documentation Elbow.transform.localRotation.eulerAngles.y returns a float value.

This code gives me the error :

There exists both implicit conversions from 'float' and 'float' and from 'float' to 'float'

This fixes it:

float currElbowAngle = LeftArm ? (float) Elbow.transform.localRotation.eulerAngles.y 
                               : 360f - Elbow.transform.localRotation.eulerAngles.y

So my question is this: What was that error trying to communicate and what actually went wrong?

Update 1: Elbow is a GameObject and this error is in Visual Studio

Upvotes: 14

Views: 469

Answers (1)

doug65536
doug65536

Reputation: 6791

The ternary (e?a:b) operator is slightly trickier for the type system, because both sides need to give the same return type. I wouldn't be surprised if there were a subtle bug there. It's good that compliers make us laugh once in a while.

This probably fixes it too:

float currElbowAngle = LeftArm ? 0.0f + Elbow.transform.localRotation.eulerAngles.y 
                               : 360f - Elbow.transform.localRotation.eulerAngles.y

I'm speculating that the trouble is that your true branch is an lvalue and the false branch is an rvalue. My workaround makes both branches an rvalue.

Upvotes: 3

Related Questions