Can the compiler produce better error message for mis-match in static func overload resolution? Should consider all equally valid overloads?

The error message is not very helpful, even misleading:

// :( Instance method 'formatted' requires the types 'Measurement<UnitTemperature>.FormatStyle.FormatInput' (aka 'Measurement<UnitTemperature>') and 'Measurement<UnitMass>' be equivalent
Measurement(value: 54.783, unit: UnitMass.kilograms).formatted(.measurement())

There is no UnitTemperature anywhere yet the compiler error message is mentioning it. It's doing this I think because there are two .measurement(...)'s:

@available(macOS 12.0, iOS 15.0, tvOS 15.0, watchOS 8.0, *)
extension FormatStyle {

    public static func measurement<UnitType>(width: Measurement<UnitType>.FormatStyle.UnitWidth, usage: MeasurementFormatUnitUsage<UnitType> = .general, numberFormatStyle: FloatingPointFormatStyle<Double>? = nil) -> Self where Self == Measurement<UnitType>.FormatStyle, UnitType : Dimension

    public static func measurement(width: Measurement<UnitTemperature>.FormatStyle.UnitWidth = .abbreviated, usage: MeasurementFormatUnitUsage<UnitTemperature> = .general, hidesScaleName: Bool = false, numberFormatStyle: FloatingPointFormatStyle<Double>? = nil) -> Self where Self == Measurement<UnitTemperature>.FormatStyle

and .measurement() (omitting the width parameter) can only matched to second one. But since there is no UnitTemperature here, the compiler should be smarter, match name only and to also suggest calling the first version of .measurement(...) for UnitMass instead?

Or maybe the problem is the inconsistency in the function signature of the two overloaded .measurement(...) with the width parameter one has default and the other not and nobody should write generic overloaded function this way? The compile knows the other .measurement(…)!

See FormatStyle.measurement(): can only work for `UnitTemparature`, other type like `UnitMass` do not compile: Why overload not used? - #2 by jrose

and (Solved) How to use Measurement.FormatStyle for generic over Measurement<UnitType: Dimension>? : for generic of Dimension, can the compiler be smart to say "hey, specify the width parameter so .measurement(....) can match to all Dimension type?

Terms of Service

Privacy Policy

Cookie Policy