-
Couldn't load subscription status.
- Fork 1.4k
3685: zio duration uses java.time.Duration #4034
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
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks really good! Great to see so few changes in the rest of the code base.
I think at this point there are only a few user facing differences:
- Because
Finiteis not a subtype ofDurationanymore but just an extractor, when pattern matching users need to docase Finite(duration) => ???rather thanduration: Finite => ???. I think this is unavoidable with the new encoding and relatively minor. - Because
Durationis no longer our data type, we have to add methods like>with extension methods. That means if you just have aDurationinstance you can't call>on it without doing something to bring the implicit syntax into scope. So right now the user needs to doimport zio.duration._. We could potentially either move this into the ZIO package object so it is in scope withimport zio._or use the trick where we give the implicit conversion the name of the type (implicit def Duration(duration: Duration): DurationOps = new DurationOps) so it is in scope when the user doesimport zio.Durationbut unfortunately I don't think any of these are ideal as a lot of times you will just have a Duration value from some combinator you are working with.
One other thing that could be good to do here is define an Ordering[Duration]. We had Duration extend Ordered[Duration] before but now we can't do that since it isn't our own data type.
|
|
||
| def apply(amount: Long, unit: TimeUnit): Duration = fromNanos(unit.toNanos(amount)) | ||
|
|
||
| def fromNanos(nanos: Long): Duration = nanos.nanos |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| def fromNanos(nanos: Long): Duration = nanos.nanos | |
| def fromNanos(nanos: Long): Duration = Finite(nanos) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I did not use Finite because if we pass a Long.MaxValue it will result in an Infinite.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also all constructors (.nanos, .millis, ...) build Duration.Zero if given negative values.
Co-authored-by: Adam Fraser <[email protected]>
Co-authored-by: Adam Fraser <[email protected]>
Co-authored-by: Adam Fraser <[email protected]>
Co-authored-by: Adam Fraser <[email protected]>
Co-authored-by: Adam Fraser <[email protected]>
Co-authored-by: Adam Fraser <[email protected]>
|
We could consider using a super lightweight "newtype" encoding to present our Albeit, I think it's common to do |
|
@adrianfilip Can you resolve conflicts so we can merge? |
|
Yes. |
|
Done |
|
💪 |
Used the suggestions from:
There were minor test changes where tests relied on types.
Also because duration is now a java.time.Duration Duration.fromNanos(Long.MaxValue) will equal Duration.Infinity.