top | item 30749921

(no title)

cytzol | 4 years 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). 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.

discuss

order

aasasd|4 years ago

Perhaps the function shouldn't accept the unit of sec². Not least because I have no idea what a delay in that unit could signify.

ironmagma|4 years ago

It 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
  )

rsa25519|4 years ago

Note that the wonderful Go type system interprets time.Second * time.Second as 277777h46m40s with the type time.Second (not sec^2)

nine_k|4 years ago

Certainly, 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).

eyelidlessness|4 years ago

This 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|4 years 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).