Skip to content

Commit 20f49f1

Browse files
ccantarero91Cristian Cantarerocb372
authored
feat: migrate to scala 3 (#532)
* feat: migrate to scala 3 * fix: removed rewrite and remove scala213 version from build.sbt * fix: removed rewrite and fmt * Rewrite kind-projector syntax to type lambda * feat: some moves from implicit to given * feat: using rewrite.scala3.removeOptionalBraces to remove braces * feat: add all the rewrites with the ends * feat: more implicit for given * fix: remove sleep doc * feat: more implicit for given & fmt * feat: PolicyDecion to enum * feat: RetryDetails to enum * fix: Remove not needed val on PolicyDecision --------- Co-authored-by: Cristian Cantarero <[email protected]> Co-authored-by: Chris Birchall <[email protected]>
1 parent cafdda3 commit 20f49f1

27 files changed

+278
-498
lines changed

Diff for: .scalafmt.conf

+9
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,12 @@ runner.dialect = scala3
33

44
align.preset = more
55
maxColumn = 110
6+
7+
rewrite {
8+
scala3 {
9+
convertToNewSyntax = true
10+
removeOptionalBraces = true
11+
insertEndMarkerMinLines = 30
12+
removeEndMarkerMaxLines = 29
13+
}
14+
}

Diff for: build.sbt

+2-8
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
import sbtcrossproject.CrossPlugin.autoImport.crossProject
2-
import _root_.org.typelevel.sbt.tpolecat.TpolecatPlugin.autoImport._
32
import _root_.org.typelevel.scalacoptions.ScalacOptions
43

5-
lazy val scalaVersion213 = "2.13.13"
6-
lazy val scalaVersion3 = "3.3.3"
7-
lazy val scalaVersions = List(scalaVersion213, scalaVersion3)
4+
lazy val scalaVersion3 = "3.3.3"
5+
lazy val scalaVersions = List(scalaVersion3)
86

97
inThisBuild(
108
Seq(
@@ -83,10 +81,6 @@ val docs = project
8381
.dependsOn(coreJVM, mtlJVM)
8482
.enablePlugins(MicrositesPlugin, BuildInfoPlugin)
8583
.settings(
86-
scalaVersion := scalaVersion213,
87-
addCompilerPlugin(
88-
"org.typelevel" %% "kind-projector" % "0.13.3" cross CrossVersion.full
89-
),
9084
tpolecatExcludeOptions ++= ScalacOptions.warnUnusedOptions,
9185
tpolecatExcludeOptions += ScalacOptions.warnNonUnitStatement,
9286
crossScalaVersions := Nil,
+7-14
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,17 @@
11
package retry
22

3-
object Fibonacci {
4-
def fibonacci(n: Int): Long = {
5-
if (n > 0)
6-
fib(n)._1
7-
else
8-
0
9-
}
3+
object Fibonacci:
4+
def fibonacci(n: Int): Long =
5+
if n > 0 then fib(n)._1
6+
else 0
107

118
// "Fast doubling" Fibonacci algorithm.
129
// See e.g. http://funloop.org/post/2017-04-14-computing-fibonacci-numbers.html for explanation.
13-
private def fib(n: Int): (Long, Long) = n match {
10+
private def fib(n: Int): (Long, Long) = n match
1411
case 0 => (0, 1)
1512
case m =>
1613
val (a, b) = fib(m / 2)
1714
val c = a * (b * 2 - a)
1815
val d = a * a + b * b
19-
if (n % 2 == 0)
20-
(c, d)
21-
else
22-
(d, c + d)
23-
}
24-
}
16+
if n % 2 == 0 then (c, d)
17+
else (d, c + d)

Diff for: modules/core/shared/src/main/scala/retry/PolicyDecision.scala

+3-9
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,6 @@ package retry
22

33
import scala.concurrent.duration.FiniteDuration
44

5-
sealed trait PolicyDecision
6-
7-
object PolicyDecision {
8-
case object GiveUp extends PolicyDecision
9-
10-
final case class DelayAndRetry(
11-
delay: FiniteDuration
12-
) extends PolicyDecision
13-
}
5+
enum PolicyDecision:
6+
case GiveUp
7+
case DelayAndRetry(delay: FiniteDuration)

Diff for: modules/core/shared/src/main/scala/retry/RetryDetails.scala

+22-22
Original file line numberDiff line numberDiff line change
@@ -2,30 +2,30 @@ package retry
22

33
import scala.concurrent.duration.FiniteDuration
44

5-
sealed trait RetryDetails {
6-
def retriesSoFar: Int
7-
def cumulativeDelay: FiniteDuration
8-
def givingUp: Boolean
9-
def upcomingDelay: Option[FiniteDuration]
10-
}
5+
enum RetryDetails(
6+
val retriesSoFar: Int,
7+
val cumulativeDelay: FiniteDuration,
8+
val givingUp: Boolean,
9+
val upcomingDelay: Option[FiniteDuration]
10+
):
1111

12-
object RetryDetails {
13-
final case class GivingUp(
12+
case GivingUp(
1413
totalRetries: Int,
1514
totalDelay: FiniteDuration
16-
) extends RetryDetails {
17-
val retriesSoFar: Int = totalRetries
18-
val cumulativeDelay: FiniteDuration = totalDelay
19-
val givingUp: Boolean = true
20-
val upcomingDelay: Option[FiniteDuration] = None
21-
}
15+
) extends RetryDetails(
16+
retriesSoFar = totalRetries,
17+
cumulativeDelay = totalDelay,
18+
givingUp = true,
19+
upcomingDelay = None
20+
)
2221

23-
final case class WillDelayAndRetry(
22+
case WillDelayAndRetry(
2423
nextDelay: FiniteDuration,
25-
retriesSoFar: Int,
26-
cumulativeDelay: FiniteDuration
27-
) extends RetryDetails {
28-
val givingUp: Boolean = false
29-
val upcomingDelay: Option[FiniteDuration] = Some(nextDelay)
30-
}
31-
}
24+
override val retriesSoFar: Int,
25+
override val cumulativeDelay: FiniteDuration
26+
) extends RetryDetails(
27+
retriesSoFar = retriesSoFar,
28+
cumulativeDelay = cumulativeDelay,
29+
givingUp = false,
30+
upcomingDelay = Some(nextDelay)
31+
)

Diff for: modules/core/shared/src/main/scala/retry/RetryPolicies.scala

+12-18
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@ package retry
33
import java.util.concurrent.TimeUnit
44

55
import cats.Applicative
6-
import cats.syntax.functor._
7-
import cats.syntax.show._
8-
import retry.PolicyDecision._
6+
import cats.syntax.functor.*
7+
import cats.syntax.show.*
8+
import retry.PolicyDecision.*
99

1010
import scala.concurrent.duration.{Duration, FiniteDuration}
1111
import scala.util.Random
1212

13-
object RetryPolicies {
13+
object RetryPolicies:
1414
private val LongMax: BigInt = BigInt(Long.MaxValue)
1515

1616
/*
@@ -24,12 +24,11 @@ object RetryPolicies {
2424
private def safeMultiply(
2525
duration: FiniteDuration,
2626
multiplier: Long
27-
): FiniteDuration = {
27+
): FiniteDuration =
2828
val durationNanos = BigInt(duration.toNanos)
2929
val resultNanos = durationNanos * BigInt(multiplier)
3030
val safeResultNanos = resultNanos min LongMax
3131
FiniteDuration(safeResultNanos.toLong, TimeUnit.NANOSECONDS)
32-
}
3332

3433
/** Don't retry at all and always give up. Only really useful for combining with other policies.
3534
*/
@@ -66,11 +65,8 @@ object RetryPolicies {
6665
def limitRetries[M[_]: Applicative](maxRetries: Int): RetryPolicy[M] =
6766
RetryPolicy.liftWithShow(
6867
{ status =>
69-
if (status.retriesSoFar >= maxRetries) {
70-
GiveUp
71-
} else {
72-
DelayAndRetry(Duration.Zero)
73-
}
68+
if status.retriesSoFar >= maxRetries then GiveUp
69+
else DelayAndRetry(Duration.Zero)
7470
},
7571
show"limitRetries(maxRetries=$maxRetries)"
7672
)
@@ -121,37 +117,35 @@ object RetryPolicies {
121117
def limitRetriesByDelay[M[_]: Applicative](
122118
threshold: FiniteDuration,
123119
policy: RetryPolicy[M]
124-
): RetryPolicy[M] = {
120+
): RetryPolicy[M] =
125121
def decideNextRetry(status: RetryStatus): M[PolicyDecision] =
126122
policy.decideNextRetry(status).map {
127123
case r @ DelayAndRetry(delay) =>
128-
if (delay > threshold) GiveUp else r
124+
if delay > threshold then GiveUp else r
129125
case GiveUp => GiveUp
130126
}
131127

132128
RetryPolicy.withShow[M](
133129
decideNextRetry,
134130
show"limitRetriesByDelay(threshold=$threshold, $policy)"
135131
)
136-
}
137132

138133
/** Add an upperbound to a policy such that once the cumulative delay over all retries has reached or
139134
* exceeded the given limit, the policy will stop retrying and give up.
140135
*/
141136
def limitRetriesByCumulativeDelay[M[_]: Applicative](
142137
threshold: FiniteDuration,
143138
policy: RetryPolicy[M]
144-
): RetryPolicy[M] = {
139+
): RetryPolicy[M] =
145140
def decideNextRetry(status: RetryStatus): M[PolicyDecision] =
146141
policy.decideNextRetry(status).map {
147142
case r @ DelayAndRetry(delay) =>
148-
if (status.cumulativeDelay + delay >= threshold) GiveUp else r
143+
if status.cumulativeDelay + delay >= threshold then GiveUp else r
149144
case GiveUp => GiveUp
150145
}
151146

152147
RetryPolicy.withShow[M](
153148
decideNextRetry,
154149
show"limitRetriesByCumulativeDelay(threshold=$threshold, $policy)"
155150
)
156-
}
157-
}
151+
end RetryPolicies

Diff for: modules/core/shared/src/main/scala/retry/RetryPolicy.scala

+16-18
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,20 @@ package retry
22

33
import cats.{Apply, Applicative, Monad, Functor}
44
import cats.kernel.BoundedSemilattice
5-
import retry.PolicyDecision._
5+
import retry.PolicyDecision.*
66

77
import scala.concurrent.duration.Duration
88
import scala.concurrent.duration.FiniteDuration
99
import cats.arrow.FunctionK
10-
import cats.implicits._
10+
import cats.implicits.*
1111
import cats.Show
1212

1313
case class RetryPolicy[M[_]](
1414
decideNextRetry: RetryStatus => M[PolicyDecision]
15-
) {
15+
):
1616
def show: String = toString
1717

18-
def followedBy(rp: RetryPolicy[M])(implicit M: Apply[M]): RetryPolicy[M] =
18+
def followedBy(rp: RetryPolicy[M])(using M: Apply[M]): RetryPolicy[M] =
1919
RetryPolicy.withShow(
2020
status =>
2121
M.map2(decideNextRetry(status), rp.decideNextRetry(status)) {
@@ -29,7 +29,7 @@ case class RetryPolicy[M[_]](
2929
* choosing the maximum of the two delays when both of the schedules want to delay the next retry. The dual
3030
* of the `meet` operation.
3131
*/
32-
def join(rp: RetryPolicy[M])(implicit M: Apply[M]): RetryPolicy[M] =
32+
def join(rp: RetryPolicy[M])(using M: Apply[M]): RetryPolicy[M] =
3333
RetryPolicy.withShow[M](
3434
status =>
3535
M.map2(decideNextRetry(status), rp.decideNextRetry(status)) {
@@ -43,7 +43,7 @@ case class RetryPolicy[M[_]](
4343
* choosing the minimum of the two delays when both of the schedules want to delay the next retry. The dual
4444
* of the `join` operation.
4545
*/
46-
def meet(rp: RetryPolicy[M])(implicit M: Apply[M]): RetryPolicy[M] =
46+
def meet(rp: RetryPolicy[M])(using M: Apply[M]): RetryPolicy[M] =
4747
RetryPolicy.withShow[M](
4848
status =>
4949
M.map2(decideNextRetry(status), rp.decideNextRetry(status)) {
@@ -57,7 +57,7 @@ case class RetryPolicy[M[_]](
5757

5858
def mapDelay(
5959
f: FiniteDuration => FiniteDuration
60-
)(implicit M: Functor[M]): RetryPolicy[M] =
60+
)(using M: Functor[M]): RetryPolicy[M] =
6161
RetryPolicy.withShow(
6262
status =>
6363
M.map(decideNextRetry(status)) {
@@ -69,7 +69,7 @@ case class RetryPolicy[M[_]](
6969

7070
def flatMapDelay(
7171
f: FiniteDuration => M[FiniteDuration]
72-
)(implicit M: Monad[M]): RetryPolicy[M] =
72+
)(using M: Monad[M]): RetryPolicy[M] =
7373
RetryPolicy.withShow(
7474
status =>
7575
M.flatMap(decideNextRetry(status)) {
@@ -84,12 +84,12 @@ case class RetryPolicy[M[_]](
8484
status => nt(decideNextRetry(status)),
8585
show"$show.mapK(<FunctionK>)"
8686
)
87-
}
87+
end RetryPolicy
8888

89-
object RetryPolicy {
89+
object RetryPolicy:
9090
def lift[M[_]](
9191
f: RetryStatus => PolicyDecision
92-
)(implicit
92+
)(using
9393
M: Applicative[M]
9494
): RetryPolicy[M] =
9595
RetryPolicy[M](decideNextRetry = retryStatus => M.pure(f(retryStatus)))
@@ -98,30 +98,28 @@ object RetryPolicy {
9898
decideNextRetry: RetryStatus => M[PolicyDecision],
9999
pretty: => String
100100
): RetryPolicy[M] =
101-
new RetryPolicy[M](decideNextRetry) {
101+
new RetryPolicy[M](decideNextRetry):
102102
override def show: String = pretty
103103
override def toString: String = pretty
104-
}
105104

106105
def liftWithShow[M[_]: Applicative](
107106
decideNextRetry: RetryStatus => PolicyDecision,
108107
pretty: => String
109108
): RetryPolicy[M] =
110109
withShow(rs => Applicative[M].pure(decideNextRetry(rs)), pretty)
111110

112-
implicit def boundedSemilatticeForRetryPolicy[M[_]](implicit
111+
given [M[_]](using
113112
M: Applicative[M]
114113
): BoundedSemilattice[RetryPolicy[M]] =
115-
new BoundedSemilattice[RetryPolicy[M]] {
114+
new BoundedSemilattice[RetryPolicy[M]]:
116115
override def empty: RetryPolicy[M] =
117116
RetryPolicies.constantDelay[M](Duration.Zero)
118117

119118
override def combine(
120119
x: RetryPolicy[M],
121120
y: RetryPolicy[M]
122121
): RetryPolicy[M] = x.join(y)
123-
}
124122

125-
implicit def showForRetryPolicy[M[_]]: Show[RetryPolicy[M]] =
123+
given [M[_]]: Show[RetryPolicy[M]] =
126124
Show.show(_.show)
127-
}
125+
end RetryPolicy

Diff for: modules/core/shared/src/main/scala/retry/RetryStatus.scala

+2-4
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,12 @@ final case class RetryStatus(
66
retriesSoFar: Int,
77
cumulativeDelay: FiniteDuration,
88
previousDelay: Option[FiniteDuration]
9-
) {
9+
):
1010
def addRetry(delay: FiniteDuration): RetryStatus = RetryStatus(
1111
retriesSoFar = this.retriesSoFar + 1,
1212
cumulativeDelay = this.cumulativeDelay + delay,
1313
previousDelay = Some(delay)
1414
)
15-
}
1615

17-
object RetryStatus {
16+
object RetryStatus:
1817
val NoRetriesYet = RetryStatus(0, Duration.Zero, None)
19-
}

Diff for: modules/core/shared/src/main/scala/retry/Sleep.scala

+4-6
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,11 @@ import cats.effect.Temporal
44

55
import scala.concurrent.duration.FiniteDuration
66

7-
trait Sleep[M[_]] {
7+
trait Sleep[M[_]]:
88
def sleep(delay: FiniteDuration): M[Unit]
9-
}
109

11-
object Sleep {
12-
def apply[M[_]](implicit sleep: Sleep[M]): Sleep[M] = sleep
10+
object Sleep:
11+
def apply[M[_]](using sleep: Sleep[M]): Sleep[M] = sleep
1312

14-
implicit def sleepUsingTemporal[F[_]](implicit t: Temporal[F]): Sleep[F] =
13+
given [F[_]](using t: Temporal[F]): Sleep[F] =
1514
(delay: FiniteDuration) => t.sleep(delay)
16-
}

0 commit comments

Comments
 (0)