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

proposal: Go 2: avoid confusing difference between time.Time Add and Sub methods #30935

Closed
mcandre opened this issue Mar 19, 2019 · 5 comments
Closed
Labels
FrozenDueToAge Proposal v2 A language change or incompatible library change
Milestone

Comments

@mcandre
Copy link

mcandre commented Mar 19, 2019

I spent way too long scratching my head why a time Add() call accepted my duration arguments, while Sub() calls present compile errors for my duration arguments.

Apparently the original API designer thought it was a good idea to use the positive one for duration arguments and the negative one for time point arguments. This technically works, if you remember to carefully multiply values by either 1 or -1, depending on whether you in fact wish to add or subtract. And get your types right each time.

This is not exactly intuitive. I would like to see these methods disambiguated into distinct AddDuration, SubDuraction, AddTime, SubTime calls, to clear up any type confusion.

@bradfitz
Copy link
Contributor

Add and Sub do different things. Maybe Sub should've been called something longer, but then it would've been longer. I think it's fine to read the docs, discover that Add and Sub are different, and then move on.

@ianlancetaylor
Copy link
Contributor

Expanding slightly, AddTime would be pointless; there is no good reason to add two time.Time values. And if we have AddDuration there is no reason to have SubDuration, since that is the same as AddDuration with the - operator. So that leaves us with SubTime and AddDuration, which is what we have today, under shorter names. And as you've discovered, they can't be confused with each other, since they take different types.

@ianlancetaylor ianlancetaylor changed the title Go 2: Fix time API proposal: Go 2: fix time API Mar 19, 2019
@gopherbot gopherbot added this to the Proposal milestone Mar 19, 2019
@ianlancetaylor ianlancetaylor added v2 A language change or incompatible library change and removed Proposal labels Mar 19, 2019
@ianlancetaylor ianlancetaylor changed the title proposal: Go 2: fix time API proposal: Go 2: avoid confusing difference between time.Time Add and Sub methods Mar 19, 2019
@beoran
Copy link

beoran commented Mar 20, 2019

@mcandre Perhaps this calls for even better documentation? Are you familiar with go doc? If you run go doc time.Add and go doc time.Sub then is the documentation you see there sufficient to make clear how to use the API or not?

@deanveloper
Copy link

deanveloper commented Mar 23, 2019

Perhaps Sub should be called something like Diff instead?

@ianlancetaylor
Copy link
Contributor

There is no support for this change. Perhaps we should have chosen different names initially, but the ones we have now are not wrong and people are familiar with them.

@golang golang locked and limited conversation to collaborators Apr 15, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
FrozenDueToAge Proposal v2 A language change or incompatible library change
Projects
None yet
Development

No branches or pull requests

6 participants