I would posit that four of the most important axioms related to implicit conversion would be:
- If x is some abstract quantity, Tx is the T which represents it best (not necessarily exactly), and T is implicitly convertible to U, then "(U)Tx" should yield either the U which best represents that same abstract quantity, or something essentially as good. The implicit conversion from "float->double" violates this axiom; conversions from "Decimal->double" or "double->float" would not
- For any instance Tx of type T, if T is implicitly convertible to U and U to V, then (V)(U)Tx should yield (V)Tx. This axiom fails for e.g. long->float->double. If implicit float->double were eliminated, this axiom could apply even if "Decimal->double" and "double->float" were added [though the addition of other types which were convertible to "double" might violate it]. Note also that proper behavior of this axiom would require that conversion from longer types directly to float be performed as though the value were first cast to double. This would mean that (float)someLong might not yield the float which was closest to someLong, but the difference between that and the closest float would be negligible by "float" standards.
- Given instances Tx and Ty of any type T, and any type U, the expression "((U)Tx == Ty) == (Tx == Ty)" should refuse to compile, throw an exception in the typecast to U, or else return true. All floating-point conversions will violate this axiom if the operands of "==" are implicitly convertible.
- Given any mix of variables of any types, if "a==b", "b==c", and "a==c" all compile, and if any two are true, the third must be as well. The implicit conversions from "int->float" and "long->double" violate this axiom, as does (for .NET) the implicit conversion from `char[]` to `String`. This is the most significant of the axioms which would be violated by "double->float".
What makes the decision to choose conversion rules based upon axiom #4 even more unfortunate is that its main effect is to help the compiler disambiguate code which the compiler should regard as ambiguous. It's far from clear whether a programmer who writes "someFloat == someDouble" wants to check whether "someFloat" is the best "float" representation of "someDouble", or whether "someDouble" is the best "double" representation of the nominal value of "someFloat" (which may or may not bear much relation to the number that float is supposed to represent). It's true that only the latter test implements an equivalence relation, but that doesn't imply that it's what the programmer's actually trying to test.
The combination of allowing implicit float->double but forbidding double->float means that code gets cluttered with lots of typecasts in cases where the behavior of the code in the absence of typecasts would be clearer than its behavior with, and yet code which is almost certainly wrong compiles without complaint. Consider, for example, which of these is more worthy of a compiler warning:
float oneTenthFloat = 1.0/10.0; // Assume this compiled as though the typecast were present
double oneTenthDouble =1.0f/10.0f;
The obvious intention of the the first statement would be to set oneTenthFloat to the "float" value which is closest to 1/10, i.e. 13,421,773/134,217,728. That's precisely what the statement would do if implicit casts were legal in such case, and there would be no plausible reason for the programmer to expect anything else. The second statement in C would set oneTenthDouble to the "double" value best represents 1/10; it's off by 5.6E-18. In C# it sets it to a "double" value which is off by 1.4E-9, i.e. more than 268 million times as much.
While there are limits to what can be done to .NET at this stage, there are some changes that could help. For example, a language could include new compile-time-only types which would be run-time equivalents to the existing primitives, but had different implicit casting behaviors (much the way "Dynamic" is really "Object" under the hood). Code which as written to use the new types could benefit from the improved semantics and diagnostics, while existing code would continue to run as it always has. The types and some of their conversion operators could be tagged to indicate that their use should be forbidden in cases where a value of the original type is passed the nth parameter of a function, and the function has an overload with the appropriate number of parameters whose nth parameter can take either the original type (without regard for whether that overload would fit the types of any other parameters).