Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

spec: Multiplication of Durations by integers makes no sense #23708

Closed
mpnally opened this issue Feb 5, 2018 · 3 comments
Closed

spec: Multiplication of Durations by integers makes no sense #23708

mpnally opened this issue Feb 5, 2018 · 3 comments

Comments

@mpnally
Copy link

mpnally commented Feb 5, 2018

I imagine this has been said many times before, but just in case ...

If I execute the following code:

package main

import (
	"fmt"
	"time"
)

func main() {
	multiplier := 10
	duration := time.Second
	product := multiplier * duration
	fmt.Printf("%v %T", product, product)
}

You would intuitively expect to see:

10s time.Duration

Instead, golang gives you

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:

package main

import (
	"fmt"
	"time"
)

func main() {
	multiplier := time.Duration(10)
	duration := time.Second
	product := multiplier * duration
	fmt.Printf("%v %T %v %T", product, product, multiplier, multiplier)
}

go again gives the wrong answer:

10s time.Duration 10ns time.Duration

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?

@bradfitz
Copy link
Contributor

bradfitz commented Feb 5, 2018

For questions about Go, see https://golang.org/wiki/Questions.

See also https://blog.golang.org/constants for why 10 * time.Second works.

@bradfitz bradfitz closed this as completed Feb 5, 2018
@mpnally
Copy link
Author

mpnally commented Feb 5, 2018

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.

@bradfitz
Copy link
Contributor

bradfitz commented Feb 5, 2018

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 mikioh changed the title Multiplication of Durations by integers makes no sense spec: Multiplication of Durations by integers makes no sense Feb 21, 2018
@golang golang locked and limited conversation to collaborators Feb 21, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants