You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
prog.go:11:24: invalid operation: multiplier * duration (mismatched types int and time.Duration)
Intuition isn't always right, but in this case I believe it is — 10s is the correct answer and Golang got it wrong. If this isn't obvious to you, ask yourself what the right answer is if I multiply 1 foot by the integer 10 — it's 10 feet. If I execute the following code:
If you multiply 10 seconds by 10 nanoseconds, the correct answer is 100 billion square nanoseconds, not 10 seconds. This is similar to multiplying 1 foot by 10 feet — the correct answer is 10 square feet. If you think a square nanosecond is a meaningless concept, then this should perhaps give an error. Other languages get duration arithmetic right — why not Go, which in most respects seems like a well thought-out language?
The text was updated successfully, but these errors were encountered:
This doesn't seem to me to be a reasonable closure. Go did not get the numeric value wrong, it got the unit wrong. That seems to me to be unrelated to the constants topic. Nevertheless, instead of creating an issue that points out that go gets this wrong, I will instead ask the question "why does Go get this wrong" on the questions wiki.
There have been a few threads about this on the mailing list(s) in the past. The summary is that Go is not a language for working with units. It also is not possible with Go's type system to let you express that mass * velocity = momentum.
mikioh
changed the title
Multiplication of Durations by integers makes no sense
spec: Multiplication of Durations by integers makes no sense
Feb 21, 2018
I imagine this has been said many times before, but just in case ...
If I execute the following code:
You would intuitively expect to see:
Instead, golang gives you
Intuition isn't always right, but in this case I believe it is — 10s is the correct answer and Golang got it wrong. If this isn't obvious to you, ask yourself what the right answer is if I multiply 1 foot by the integer 10 — it's 10 feet. If I execute the following code:
go again gives the wrong answer:
If you multiply 10 seconds by 10 nanoseconds, the correct answer is 100 billion square nanoseconds, not 10 seconds. This is similar to multiplying 1 foot by 10 feet — the correct answer is 10 square feet. If you think a square nanosecond is a meaningless concept, then this should perhaps give an error. Other languages get duration arithmetic right — why not Go, which in most respects seems like a well thought-out language?
The text was updated successfully, but these errors were encountered: