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
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.
The text was updated successfully, but these errors were encountered:
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.
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
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
@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?
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.
I spent way too long scratching my head why a time
Add()
call accepted my duration arguments, whileSub()
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.The text was updated successfully, but these errors were encountered: