Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 954
Perchik Avatar asked Jan 16 '13 18:01

Perchik


1 Answers

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.

like image 163
doug65536 Avatar answered Oct 21 '22 22:10

doug65536