9 ms·
Like many things with Go, its approach seems reasonable and simple at first, but allows you to accidentally write code that looks right but is very, very wrong.
by cytzol 5y ago
Like many things with Go, its approach seems reasonable and simple at first, but allows you to accidentally write code that looks right but is very, very wrong. For example, what do you think this code will do?
delaySecs := 1 * time.Second
time.Sleep(delaySecs * time.Second)
Now I insist on using the durationcheck lint to guard against this (https://github.com/charithe/durationcheck https://github.com/charithe/durationcheck). It found a flaw in some exponential-backoff code I had refactored but couldn’t easily fully test that looked right but was wrong, and now I don’t think Go’s approach is reasonable anymore.
- deleted 5y ago[deleted]
- aasasd 5y agoPerhaps the function shouldn't accept the unit of sec². Not least because I have no idea what a delay in that unit could signify.
- rsa25519 5y agoNote that the wonderful Go type system interprets time.Second * time.Second as 277777h46m40s with the type time.Second (not sec^2)
- frozenice 5y agotime.Second * time.Second The type of this is `time.Duration` (or int64 internally), not `time.Second` (which is a const with a value). I agree, though, that this is not quite sound, because it can be misused, as shown above with `time.Sleep(delaySecs * time.Second)`. In Kotlin you can do `1.seconds + 1.minutes` but not `1.seconds * 1.minutes` (compilation error), which I quite like. Here is a playground link: https://pl.kotl.in/YZLu97AY8 https://pl.kotl.in/YZLu97AY8
- ironmagma 5y agoIt doesn't actually use units. Everything is in nanoseconds, so time.Second is just another unitless number. const ( Nanosecond Duration = 1 Microsecond = 1000 * Nanosecond Millisecond = 1000 * Microsecond Second = 1000 * Millisecond Minute = 60 * Second Hour = 60 * Minute )
- nine_k 5y agoCertainly, but for that the type system should be rich enough to support unit designators. I know how to implement that in Haskell, and that it can be implemented in C++ and Rust. I know how to logically implement that in Java or Typescript, but usability will suck (no infix operators).
- int_19h 5y agoGo tends to cover such things by incorporating them directly in the language. But then it tends to not cover them at all because it would "overcomplicate" the language... For a good example of what it looks like when somebody does bother to do it, see F# units of measure.
- eyelidlessness 5y agoThis looks to me like the semantics are good but the implementation details are broken. 1 * time.Second * time.Second semantically reads to me as 1 second. If time.Second is some numeric value, that’s obviously wrong everwhere unless the type system reflects and enforces the unit conversion.
- linkdd 5y ago> 1 * time.Second * time.Second semantically reads to me as 1 second. Which is wrong, 1s * 1s = 1s². For example, the force of gravity is expressed in m/s² and describe an acceleration (m/s / s, aka a change of velocity per time units, where velocity is a change of distance per time units).
- deleted 5y ago[deleted]
- eyelidlessness 5y agoOkay so do I need to consult Relativity to program 1sec + 2min?
- linkdd 5y agoSince 1min could be 61 seconds[1], yes? But assuming your comment is not a joke. You probably want to convert minutes to seconds in order to work with the same units, then add the scalar parts together. That's how you deal with different quantities: convert to same unit, add values. This is analog to fractions: 1/2 + 1/4 = 2/4 + 1/4 = (2+1)/4 = 3/4. [1] - https://en.wikipedia.org/wiki/Leap_second
- eyelidlessness 5y agoIn basic middle school math it’s common to multiply different units as a basic conversion mechanism. Multiplying by the same unit is semantically equivalent to “x times 1 is identity(x)”, and other cross-unit arithmetic implies conversion to ensure like units before processing. A typed unit numeric system would imply that to me. It would not imply I’m multiplying the units, but rather the scalar value of the unit.
- deleted 5y ago[deleted]