4 ms·
ValueMeters isn't a primitive type, it's just convertible to one. x := 1.0 y := ValueMeters(1.0) x + y // Mismatched types error - ValueMetres and
by parenthephobia 10y ago
ValueMeters isn't a primitive type, it's just convertible to one.
x := 1.0
y := ValueMeters(1.0)
x + y // Mismatched types error - ValueMetres and float64
Because Go doesn't believe in operator overloading, there isn't even anything you can do about it. You have to abandon the pretence and explicitly convert the values to the same type.
Go does a little magic with literals in that it will implicitly use explicit conversions on them to satisfy type constraints, so ValueMetres(1) + 1 works, but ValueMetres(1) + float64(1) does not. (Otherwise you end up with annoyances like not being able to add "1" to a float.)
This gets particularly gnarly when interacting with libraries which expect the underlying type you're wrapping:
x := ValueMetres(math.Sin(float64(x)))