3 ms·
> How much more complicated does the Go code get if you want to allow for addition of inches and feet, but still prevent nonsensical things like adding inches a
by pcwalton 13y ago
> How much more complicated does the Go code get if you want to allow for addition of inches and feet, but still prevent nonsensical things like adding inches and seconds?
You can't do it, because Go doesn't have operator overloading.
Also, I'd argue that the automatic promotion of bare numbers to a wrapped type is not really "a matter of taste" but rather severely hurts the ability of Go's newtype feature to catch bugs. In languages that have stricter newtypes, such as ML or Haskell, you can design your APIs such that the caller has to think about which unit to use. Someone using your code might accidentally write the number "12" to mean 12 inches when your API takes feet, but the Go compiler will silently let this mistake pass.
- jerf 13y ago"Also, I'd argue that the automatic promotion of bare numbers to a wrapped type is not really "a matter of taste" but rather severely hurts the ability of Go's newtype feature to catch bugs." I agree, but in general, Go is a pragmatic language rather than a pure one. In Haskell it would certainly be the wrong choice. You can pull some fun tricks in Go, such as http://www.jerf.org/iri/post/2923 http://www.jerf.org/iri/post/2923 , but it's not interested in a Haskellian "correctness" in general.
- pcwalton 13y agoI see nothing pragmatic about the automatic wrapping in Go, though. It feels like a pure loss--language complexity, safety, correctness--over not doing it.