-
Notifications
You must be signed in to change notification settings - Fork 17.7k
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: time: make an uninitialized Timer usable #12721
Comments
Edit: this is obsolete given edits I made to the original post.
t := time.NewTimer(math.MaxInt64)
t.Stop() |
Notice that everything in the time package refers to a Timer as We probably don't want to do anything here. The caller can use an var t *time.Timer
for {
if t == nil {
t = time.NewTimer(...)
}
... |
@bradfitz sure, What I'm going for is similar to |
Another option is to use t := time.NewTimer(0)
<-t.C |
Thanks @fjl; I've updated my post to remove the complex workaround and took your suggestion. |
|
There are two kinds of timer: those with an associated func and those with an associated channel. There is no API to change or decide that detail after the timer has been created. That is, a zero Timer would do nothing at all - call no function and send on no channel - when it expires. We could make a zero timer valid for calls to Reset and Stop, but you'd never know when it went off, so what's the point? If you need a timer that isn't going off any time soon, initialize with time.NewTimer(525600*time.Minute) (or your favorite long amount of time) and then Stop it. Or use nil, as @bradfitz suggested above. There's no need for new API here. |
@rsc good point, I hadn't considered the AfterFunc/NewTimer dichotomy. I was only thinking of channel timers (not func timers). I think what I'd want is for Stop to do nothing for zero timers and for Reset to associate a channel-sending function (sendTime, in the code) with the timer. In other words, the effect of
would be the same as
It seems to me that preferring "channel timers" over "func timers" in this way is okay since func timers are much less frequently used, and because the documentation already casts func timers as a special case:
|
Since this has been open for a while and doesn't seem to have any support, I'll go ahead and close for now. |
Change https://golang.org/cl/14871 mentions this issue: |
Alternatively, add a new function to package time:
It is common to reuse a timer via Reset several times (usually in a loop); often in these scenarios there's no initial value needed for the timer. It would be nice to be able to do this:
but today any use of a timer that isn't created using
NewTimer
(orAfterFunc
) panics.Given #11513, almost any "obvious" way of accomplishing this is incorrect:
You need to do something like this instead:
but it's unlikely that most people will know that something like this is required, because the bugs in the above versions can be hard to notice.
Compared with
t := time.NewTimer(0); <-t.C
, eithervar t time.Timer
ortime.NewStoppedTimer()
are clearer, and the former doesn't even require expanding the time API.(Note: edited to remove overly-complex workaround.)
The text was updated successfully, but these errors were encountered: