Skip to content

Commit

Permalink
use new Scala 3 syntax. prepare latest Scala 3
Browse files Browse the repository at this point in the history
  • Loading branch information
xuwei-k committed Aug 26, 2024
1 parent d8321e9 commit d83b1a8
Show file tree
Hide file tree
Showing 17 changed files with 41 additions and 31 deletions.
10 changes: 10 additions & 0 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,16 @@ lazy val buildSettings = Seq(
"-deprecation"
) ++ { if (tlIsScala3.value) Seq() else Seq("-Xfatal-warnings") }, // Scala 3 doesn't support -Wconf
Compile / console / scalacOptions -= "-Ywarn-unused:imports",
scalacOptions ++= {
scalaBinaryVersion.value match {
case "2.12" =>
Seq("-Xsource:3")
case "2.13" =>
Seq("-Xsource:3-cross")
case _ =>
Nil
}
},
scalacOptions ++= {
if (tlIsScala3.value)
Seq(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ private[focus] trait KeywordParserBase extends ParserBase {
Some(
Name(keyword),
FromType(code.tpe.widen),
TypeArgs(inferredTypeArgs.map(_.tpe): _*),
TypeArgs(inferredTypeArgs.map(_.tpe)*),
ValueArgs(),
RemainingCode(code)
)
Expand All @@ -32,8 +32,8 @@ private[focus] trait KeywordParserBase extends ParserBase {
Some(
Name(keyword),
FromType(code.tpe.widen),
TypeArgs(inferredTypeArgs.map(_.tpe): _*),
ValueArgs(valueArgs: _*),
TypeArgs(inferredTypeArgs.map(_.tpe)*),
ValueArgs(valueArgs*),
RemainingCode(code)
)

Expand All @@ -42,7 +42,7 @@ private[focus] trait KeywordParserBase extends ParserBase {
Some(
Name(keyword),
FromType(code.tpe.widen),
TypeArgs(directTypeArgs.map(_.tpe): _*),
TypeArgs(directTypeArgs.map(_.tpe)*),
ValueArgs(),
RemainingCode(code)
)
Expand All @@ -53,7 +53,7 @@ private[focus] trait KeywordParserBase extends ParserBase {
Some(
Name(keyword),
FromType(code.tpe.widen),
TypeArgs(allTypeArgs.map(_.tpe): _*),
TypeArgs(allTypeArgs.map(_.tpe)*),
ValueArgs(),
RemainingCode(code)
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import monocle.internal.focus.FocusBase
import monocle.internal.focus.features.KeywordParserBase

private[focus] trait AsParser {
this: FocusBase with KeywordParserBase =>
this: FocusBase & KeywordParserBase =>

object KeywordAs extends FocusParser {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import monocle.internal.focus.FocusBase
import monocle.internal.focus.features.KeywordParserBase

private[focus] trait AtParser {
this: FocusBase with KeywordParserBase =>
this: FocusBase & KeywordParserBase =>

object KeywordAt extends FocusParser {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import monocle.internal.focus.FocusBase
import monocle.internal.focus.features.KeywordParserBase

private[focus] trait EachParser {
this: FocusBase with KeywordParserBase =>
this: FocusBase & KeywordParserBase =>

object KeywordEach extends FocusParser {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import monocle.internal.focus.FocusBase
import monocle.internal.focus.features.KeywordParserBase

private[focus] trait IndexParser {
this: FocusBase with KeywordParserBase =>
this: FocusBase & KeywordParserBase =>

object KeywordIndex extends FocusParser {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import monocle.internal.focus.FocusBase
import monocle.internal.focus.features.SelectParserBase

private[focus] trait SelectFieldParser {
this: FocusBase with SelectParserBase =>
this: FocusBase & SelectParserBase =>

import this.macroContext.reflect._

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import monocle.internal.focus.FocusBase
import monocle.internal.focus.features.SelectParserBase

private[focus] trait SelectOnlyFieldParser {
this: FocusBase with SelectParserBase =>
this: FocusBase & SelectParserBase =>

import this.macroContext.reflect._

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import monocle.internal.focus.FocusBase
import monocle.internal.focus.features.KeywordParserBase

private[focus] trait SomeParser {
this: FocusBase with KeywordParserBase =>
this: FocusBase & KeywordParserBase =>

object KeywordSome extends FocusParser {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import monocle.internal.focus.FocusBase
import monocle.internal.focus.features.KeywordParserBase

private[focus] trait WithDefaultParser {
this: FocusBase with KeywordParserBase =>
this: FocusBase & KeywordParserBase =>

object KeywordWithDefault extends FocusParser {

Expand Down
12 changes: 6 additions & 6 deletions core/shared/src/main/scala/monocle/Fold.scala
Original file line number Diff line number Diff line change
Expand Up @@ -35,31 +35,31 @@ trait Fold[S, A] extends Serializable { self =>

/** find the first target matching the predicate */
def find(p: A => Boolean): S => Option[A] =
foldMap(a => Some(a).filter(p))(_)(Monoids.firstOption)
foldMap(a => Some(a).filter(p))(_)(using Monoids.firstOption)

/** get the first target */
def headOption(s: S): Option[A] =
foldMap(Option(_))(s)(Monoids.firstOption)
foldMap(Option(_))(s)(using Monoids.firstOption)

/** get the last target */
def lastOption(s: S): Option[A] =
foldMap(Option(_))(s)(Monoids.lastOption)
foldMap(Option(_))(s)(using Monoids.lastOption)

/** check if at least one target satisfies the predicate */
def exist(p: A => Boolean): S => Boolean =
foldMap(p(_))(_)(Monoids.any)
foldMap(p(_))(_)(using Monoids.any)

/** check if all targets satisfy the predicate */
def all(p: A => Boolean): S => Boolean =
foldMap(p(_))(_)(Monoids.all)
foldMap(p(_))(_)(using Monoids.all)

/** calculate the number of targets */
def length(s: S): Int =
foldMap(_ => 1)(s)

/** check if there is no target */
def isEmpty(s: S): Boolean =
foldMap(_ => false)(s)(Monoids.all)
foldMap(_ => false)(s)(using Monoids.all)

/** check if there is at least one target */
def nonEmpty(s: S): Boolean =
Expand Down
2 changes: 1 addition & 1 deletion core/shared/src/main/scala/monocle/Traversal.scala
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ trait PTraversal[S, T, A, B] extends PSetter[S, T, A, B] with Fold[S, A] { self
*/
def parModifyF[F[_]](f: A => F[B])(s: S)(implicit F: Parallel[F]): F[T] =
F.sequential(
modifyA(a => F.parallel(f(a)))(s)(F.applicative)
modifyA(a => F.parallel(f(a)))(s)(using F.applicative)
)

override def some[A1, B1](implicit ev1: A =:= Option[A1], ev2: B =:= Option[B1]): PTraversal[S, T, A1, B1] =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ object FilterIndex extends FilterIndexFunctions {
.traverse { case (k, v) =>
(if (predicate(k)) f(v) else v.pure[F]).tupleLeft(k)
}
.map(kvs => SortedMap(kvs: _*)(ok.toOrdering))
.map(kvs => SortedMap(kvs *)(ok.toOrdering))
}
}

Expand Down
2 changes: 1 addition & 1 deletion example/src/test/scala-2/monocle/HttpRequestExample.scala
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ class HttpRequestExample extends MonocleSuite {

test("headers with filterIndex") {
val r = headers
.filterIndex { h: String => h.contains("timeout") }
.filterIndex((h: String) => h.contains("timeout"))
.andThen(stringToInt)
.modify(_ * 2)(r1)

Expand Down
4 changes: 2 additions & 2 deletions example/src/test/scala-2/monocle/JsonExample.scala
Original file line number Diff line number Diff line change
Expand Up @@ -218,8 +218,8 @@ class JsonExample extends MonocleSuite {
def modifyA[F[_]: Applicative](f: Json => F[Json])(a: Json): F[Json] =
a match {
case j @ (JsString(_) | JsNumber(_)) => Applicative[F].pure(j)
case JsArray(l) => l.traverse(f).map(JsArray)
case JsObject(m) => Traverse[Map[String, *]].traverse(m)(f).map(JsObject)
case JsArray(l) => l.traverse(f).map(JsArray.apply)
case JsObject(m) => Traverse[Map[String, *]].traverse(m)(f).map(JsObject.apply)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class FilterIndexExample extends MonocleSuite {
assertEquals(
SortedMap("One" -> 1, "Two" -> 2)
.focus()
.filterIndex { k: String =>
.filterIndex { (k: String) =>
k.toLowerCase.contains("o")
}
.getAll,
Expand All @@ -21,7 +21,7 @@ class FilterIndexExample extends MonocleSuite {
assertEquals(
SortedMap("One" -> 1, "Two" -> 2)
.focus()
.filterIndex { k: String =>
.filterIndex { (k: String) =>
k.startsWith("T")
}
.replace(3),
Expand All @@ -35,11 +35,11 @@ class FilterIndexExample extends MonocleSuite {
test(
"filterIndexes creates Traversal from a List, IList, Vector or Stream to all values where the index matches the predicate"
) {
assertEquals(List(1, 3, 5, 7).focus().filterIndex { i: Int => i % 2 == 0 }.getAll, List(1, 5))
assertEquals(List(1, 3, 5, 7).focus().filterIndex((i: Int) => i % 2 == 0).getAll, List(1, 5))

assertEquals(List(1, 3, 5, 7).focus().filterIndex { i: Int => i >= 2 }.modify(_ + 2), List(1, 3, 7, 9))
assertEquals(List(1, 3, 5, 7).focus().filterIndex((i: Int) => i >= 2).modify(_ + 2), List(1, 3, 7, 9))
assertEquals(
Vector(1, 3, 5, 7).focus().filterIndex { i: Int => i >= 2 }.modify(_ + 2),
Vector(1, 3, 5, 7).focus().filterIndex((i: Int) => i >= 2).modify(_ + 2),
Vector(1, 3, 7, 9)
)
}
Expand Down
4 changes: 2 additions & 2 deletions macro/src/test/scala/monocle/macros/GenPrismSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ class GenPrismSpec extends DisciplineSuite {
case class I(i: Int) extends IntOrString
case class S(s: String) extends IntOrString

implicit val iArb: Arbitrary[I] = Arbitrary(arbitrary[Int].map(I))
implicit val sArb: Arbitrary[S] = Arbitrary(arbitrary[String].map(S))
implicit val iArb: Arbitrary[I] = Arbitrary(arbitrary[Int].map(I.apply))
implicit val sArb: Arbitrary[S] = Arbitrary(arbitrary[String].map(S.apply))

implicit val intOrStringArb: Arbitrary[IntOrString] =
Arbitrary(Gen.oneOf(iArb.arbitrary, sArb.arbitrary))
Expand Down

0 comments on commit d83b1a8

Please sign in to comment.