Equivalent implicit operators: why are they legal?

后端 未结 2 1226
长情又很酷
长情又很酷 2021-01-05 11:04

Update!

See my dissection of a portion of the C# spec below; I think I must be missing something, because to me it looks like the behavior I\'

相关标签:
2条回答
  • 2021-01-05 11:29

    We don't really want it to be a compile-time error just to define conversions which might cause ambiguity. Suppose that we alter Type0 to store a double, and for some reason we want to provide separate conversions to signed integer and unsigned integer.

    class Type0
    {
        public double Value { get; private set; }
    
        public Type0(double value)
        {
            Value = value;
        }
    
        public static implicit operator Int32(Type0 other)
        {
            return (Int32)other.Value;
        }
    
        public static implicit operator UInt32(Type0 other)
        {
            return (UInt32)Math.Abs(other.Value);
        }
    
    }
    

    This compiles fine, and I can use use both conversions with

    Type0 t = new Type0(0.9);
    int i = t;
    UInt32 u = t;
    

    However, it's a compile error to try float f = t because either of the implicit conversions could be used to get to an integer type which can then be converted to float.

    We only want the compiler to complain about these more complex ambiguities when they're actually used, since we'd like the Type0 above to compile. For consistency, the simpler ambiguity should also cause an error at the point you use it rather than when you define it.

    EDIT

    Since Hans removed his answer which quoted the spec, here's a quick run through the part of the C# spec that determines whether a conversion is ambiguous, having defined U to be the set of all the conversions which could possibly do the job:

    • Find the most specific source type, SX, of the operators in U:
      • If any of the operators in U convert from S, then SX is S.
      • Otherwise, SX is the most encompassed type in the combined set of target types of the operators in U. If no most encompassed type can be found, then the conversion is ambiguous and a compile-time error occurs.

    Paraphrased, we prefer a conversion which converts directly from S, otherwise we prefer the type which is "easiest" to convert S to. In both examples, we have two conversions from S available. If there were no conversions from Type2, we'd prefer a conversion from Type0 over one from object. If no one type is obviously the better choice to convert from, we fail here.

    • Find the most specific target type, TX, of the operators in U:
      • If any of the operators in U convert to T, then TX is T.
      • Otherwise, TX is the most encompassing type in the combined set of target types of the operators in U. If no most encompassing type can be found, then the conversion is ambiguous and a compile-time error occurs.

    Again, we'd prefer to convert directly to T, but we'll settle for the type that's "easiest" to convert to T. In Dan's example, we have two conversions to T available. In my example, the possible targets are Int32 and UInt32, and neither is a better match than the other, so this is where the conversion fails. The compiler has no way to know whether float f = t means float f = (float)(Int32)t or float f = (float)(UInt32)t.

    • If U contains exactly one user-defined conversion operator that converts from SX to TX, then this is the most specific conversion operator. If no such operator exists, or if more than one such operator exists, then the conversion is ambiguous and a compile-time error occurs.

    In Dan's example, we fail here because we have two conversions left from SX to TX. We could have no conversions from SX to TX if we chose different conversions when deciding SX and TX. For example, if we had a Type1a derived from Type1, then we might have conversions from Type2 to Type1a and from Type0 to Type1 These would still give us SX=Type2 and TX=Type1, but we don't actually have any conversion from Type2 to Type1. This is OK, because this really is ambiguous. The compiler doesn't know whether to convert Type2 to Type1a and then cast to Type1, or cast to Type0 first so that it can use that conversion to Type1.

    0 讨论(0)
  • 2021-01-05 11:42

    Ultimately, it can't be prohibitted with complete success. You and I could publish two assemblies. They we could start using each other's assembles, while updating our own. Then we could each provide implicit casts between types defined in each assembly. Only when we release the next version, could this be caught, rather than at compile time.

    There's an advantage in not trying to ban things that can't be banned, as it makes for clarity and consistency (and there's a lesson for legislators in that).

    0 讨论(0)
提交回复
热议问题