Skip to content

Commit ceb7593

Browse files
authored
Merge pull request #1061 from yadavan88/scala-core-migration-scala3
Scala 3 Migration of modules
2 parents 6f78fcd + 06405ce commit ceb7593

File tree

228 files changed

+80
-77
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

228 files changed

+80
-77
lines changed

build.sbt

+26-12
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,10 @@ val zioVersion = "2.0.19"
2525
lazy val scala_core = (project in file("scala-core-modules/scala-core"))
2626
.settings(
2727
name := "scala-core",
28+
scalaVersion := scala3Version,
2829
libraryDependencies ++=
2930
Seq(
30-
jUnitInterface,
31-
catsEffect
31+
jUnitInterface
3232
) ++ scalaTestDeps
3333
)
3434

@@ -43,15 +43,32 @@ lazy val scala_core_2 = (project in file("scala-core-modules/scala-core-2"))
4343
lazy val scala_core_3 = (project in file("scala-core-modules/scala-core-3"))
4444
.settings(
4545
name := "scala-core-3",
46+
scalaVersion := scala3Version,
4647
libraryDependencies ++= scalaTestDeps,
4748
libraryDependencies += jUnitInterface,
48-
libraryDependencies += scalaReflection,
4949
libraryDependencies += "org.scala-lang.modules" %% "scala-xml" % "2.2.0"
5050
)
5151

52+
lazy val scala2_core = (project in file("scala-core-modules/scala2-core"))
53+
.settings(
54+
name := "scala2-core",
55+
libraryDependencies ++= scalaTestDeps,
56+
libraryDependencies += jUnitInterface,
57+
libraryDependencies += scalaReflection,
58+
libraryDependencies += "org.scala-lang.modules" %% "scala-xml" % "2.2.0",
59+
libraryDependencies += "com.github.scopt" %% "scopt" % "4.1.0",
60+
libraryDependencies += "org.rogach" %% "scallop" % "5.0.1",
61+
libraryDependencies += "org.backuity.clist" %% "clist-core" % "3.5.1",
62+
libraryDependencies += "org.backuity.clist" %% "clist-macros" % "3.5.1" % "provided",
63+
libraryDependencies += "args4j" % "args4j" % "2.33",
64+
libraryDependencies += "org.scala-lang" % "scala-compiler" % scalaVersion.value % "test",
65+
libraryDependencies += catsEffect
66+
)
67+
5268
lazy val scala_core_4 = (project in file("scala-core-modules/scala-core-4"))
5369
.settings(
5470
name := "scala-core-4",
71+
scalaVersion := scala3Version,
5572
libraryDependencies ++= scalaTestDeps,
5673
libraryDependencies += jUnitInterface,
5774
libraryDependencies += scalaReflection
@@ -60,36 +77,32 @@ lazy val scala_core_4 = (project in file("scala-core-modules/scala-core-4"))
6077
lazy val scala_core_5 = (project in file("scala-core-modules/scala-core-5"))
6178
.settings(
6279
name := "scala-core-5",
80+
scalaVersion := scala3Version,
6381
libraryDependencies ++= scalaTestDeps,
64-
libraryDependencies += jUnitInterface,
65-
libraryDependencies += scalaReflection
82+
libraryDependencies += jUnitInterface
6683
)
6784

6885
lazy val scala_core_6 = (project in file("scala-core-modules/scala-core-6"))
6986
.settings(
7087
name := "scala-core-6",
88+
scalaVersion := scala3Version,
7189
libraryDependencies ++= scalaTestDeps,
7290
libraryDependencies += jUnitInterface
7391
)
7492

7593
lazy val scala_core_7 = (project in file("scala-core-modules/scala-core-7"))
7694
.settings(
7795
name := "scala-core-7",
96+
scalaVersion := scala3Version,
7897
libraryDependencies ++= scalaTestDeps,
7998
libraryDependencies += jUnitInterface,
80-
libraryDependencies += "com.github.scopt" %% "scopt" % "4.1.0",
81-
libraryDependencies += "org.rogach" %% "scallop" % "5.0.1",
82-
libraryDependencies += "org.backuity.clist" %% "clist-core" % "3.5.1",
83-
libraryDependencies += "org.backuity.clist" %% "clist-macros" % "3.5.1" % "provided",
84-
libraryDependencies += "args4j" % "args4j" % "2.33"
8599
)
86100

87101
lazy val scala_core_8 = (project in file("scala-core-modules/scala-core-8"))
88102
.settings(
89103
name := "scala-core-8",
90-
libraryDependencies += scalaReflection,
91104
libraryDependencies ++= scalaTestDeps,
92-
libraryDependencies += "org.scala-lang" % "scala-compiler" % scalaVersion.value % "test",
105+
scalaVersion := scala3Version,
93106
libraryDependencies += "org.scala-lang.modules" %% "scala-java8-compat" % "1.0.2",
94107
libraryDependencies += "com.typesafe" % "config" % "1.2.1"
95108
// scalacOptions += "-Ymacro-debug-lite"
@@ -106,6 +119,7 @@ lazy val scala_core_io = (project in file("scala-core-modules/scala-core-io"))
106119
lazy val scala_core_oop = (project in file("scala-core-modules/scala-core-oop"))
107120
.settings(
108121
name := "scala-core-oop",
122+
scalaVersion := scala3Version,
109123
libraryDependencies ++=
110124
Seq(catsEffect, jUnitInterface) ++ scalaTestDeps
111125
)

scala-core-modules/scala-core-2/src/test/scala/com/baeldung/scala/exceptionhandling/ExceptionHandlingUnitTest.scala

+1-6
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,6 @@
11
package com.baeldung.scala.exceptionhandling
22

3-
import com.baeldung.scala.exceptionhandling.ExceptionHandling.{
4-
DivideByZero,
5-
divideWithEither,
6-
divideWithOption,
7-
divideWithTry
8-
}
3+
import com.baeldung.scala.exceptionhandling.ExceptionHandling._
94
import org.scalatest.flatspec.AnyFlatSpec
105
import org.scalatest.matchers.should.Matchers
116

+3-3
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,19 @@ class TaskUnitTest extends AnyFlatSpec with Matchers {
88
"Task" should "be instantiated with default constructor" in {
99
val task = new Task("do something")
1010
task.description should be("do something")
11-
task.status should be("pending")
11+
task.status() should be("pending")
1212
}
1313

1414
it should "be able to construct without new keywork" in {
1515
val task = Task("do something")
1616
task.description should be("do something")
17-
task.status should be("pending")
17+
task.status() should be("pending")
1818
}
1919

2020
it should "have a contructor with status parameter" in {
2121
val task = Task("do something", "started")
2222
task.description should be("do something")
23-
task.status should be("started")
23+
task.status() should be("started")
2424
}
2525

2626
it should "extract status" in {

scala-core-modules/scala-core-4/README.md

-1
+3-2
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,18 @@
11
package com.baeldung.scala.structuraltypes
22

33
import scala.io.Source
4+
import scala.language.reflectiveCalls
45

56
trait ResourceClosing {
67
type Closable = { def close(): Unit }
78

8-
def using(resource: Closable)(fn: () => Unit) {
9+
def using(resource: Closable)(fn: () => Unit) = {
910
try {
1011
fn()
1112
} finally { resource.close() }
1213
}
1314

14-
def using(file: Source)(fn: () => Unit) {
15+
def using(file: Source)(fn: () => Unit) = {
1516
try {
1617
fn()
1718
} finally { file.close() }
+2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ package com.baeldung.scala.structuraltypes
33
import org.scalatest.flatspec.AnyFlatSpec
44
import org.scalatest.matchers.should.Matchers
55

6+
import scala.language.reflectiveCalls
7+
68
class DuckUnitTest extends AnyFlatSpec with Matchers {
79
type Flyer = { def fly(): Unit }
810
def callFly(thing: Flyer): Unit = thing.fly()
+3-3
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ object MegamorphicCallsite extends App {
66
fun(n) // callsite
77
}
88

9-
val f1 = { n: Int => n * n }
10-
val f2 = { n: Int => n + n }
11-
val f3 = { n: Int => n }
9+
val f1 = { (n: Int) => n * n }
10+
val f2 = { (n: Int) => n + n }
11+
val f3 = { (n: Int) => n }
1212

1313
execute(f1)(5)
1414
execute(f2)(5)
+3-1
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,7 @@ object VectorBasics extends App {
77
val vecAppended: Vector[Int] = vec :+ 4
88
val vecPrepended: Vector[Int] = 0 +: vec
99

10-
println(vec, vecPrepended, vecAppended)
10+
println(vec)
11+
println(vecPrepended)
12+
println(vecAppended)
1113
}

scala-core-modules/scala-core-6/README.md

-1

scala-core-modules/scala-core-7/README.md

-1

scala-core-modules/scala-core-8/README.md

-2
+3
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,6 @@ case class GuitarBuilder private (
3131
delay = delay
3232
)
3333
}
34+
object GuitarBuilder {
35+
def apply(): GuitarBuilder = new GuitarBuilder()
36+
}

scala-core-modules/scala-core-oop/README.md

-1
+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ object ObjectExample extends App {
2020
}
2121

2222
// Throws exception in case we call FlyingObject other than airplane and bird
23-
def nonExhaustive(objects: FlyingObject.Value) {
23+
def nonExhaustive(objects: FlyingObject.Value) = {
2424
objects match {
2525
case FlyingObject.airplane => println("I am an airplane")
2626
case FlyingObject.bird => println("I am a bird")
+3-3
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,15 @@ object ClassExamples {
55
val constA = "A"
66
val constB = 4
77
class Abc(var a: String, var b: Int) {
8-
def this(a: String) {
8+
def this(a: String) = {
99
this(a, constB)
1010
this.a = a
1111
}
12-
def this(b: Int) {
12+
def this(b: Int) = {
1313
this(constA, b)
1414
this.b = b
1515
}
16-
def this() {
16+
def this() = {
1717
this(constA, constB)
1818
}
1919
}

scala-core-modules/scala-core/README.md

-2

scala-core-modules/scala-core/src/main/scala-2/com/baeldung/scala/traits/Score.scala renamed to scala-core-modules/scala-core/src/main/scala/com/baeldung/scala/traits/Score.scala

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ class Score(
1818

1919
override def produce(): String = s"The score is produced by $engineer"
2020

21-
override def algorithm(): MixingAlgorithm = {
21+
override def algorithm: MixingAlgorithm = {
2222
if (qualityRatio < 3) LowInstrumentalQuality
2323
else super.algorithm
2424
}

scala-core-modules/scala-core/src/test/scala-2/com/baeldung/scala/traits/ScoreUnitTest.scala renamed to scala-core-modules/scala-core/src/test/scala/com/baeldung/scala/traits/ScoreUnitTest.scala

+2-2
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ class ScoreUnitTest {
4646
val studio = "Abbey Studios"
4747
val score = new Score(composer, engineer, orchestra, mixer, 1, studio)
4848

49-
assertEquals(score.algorithm().toString, "Low instrumental quality")
49+
assertEquals(score.algorithm.toString, "Low instrumental quality")
5050
}
5151

5252
@Test
@@ -59,7 +59,7 @@ class ScoreUnitTest {
5959
val studio = "Abbey Studios"
6060
val score = new Score(composer, engineer, orchestra, mixer, 10, studio)
6161

62-
assertEquals(score.algorithm().toString, "High instrumental quality")
62+
assertEquals(score.algorithm.toString, "High instrumental quality")
6363
}
6464

6565
@Test

scala-core-modules/scala-core/src/test/scala-2/com/baeldung/scala/tuples/TuplesUnitTest.scala renamed to scala-core-modules/scala-core/src/test/scala/com/baeldung/scala/tuples/TuplesUnitTest.scala

+3-2
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,9 @@ class TuplesUnitTest extends AnyWordSpec with Matchers {
1616
name shouldBe "Joe"
1717
age shouldBe 34
1818
}
19-
"not contain more then 22 elements" in {
20-
"(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23)" shouldNot compile
19+
// From Scala3 onwards, we can have more than 22 fields in tuple.
20+
"allow more then 22 elements in Scala 3" in {
21+
"(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23)" should compile
2122
}
2223
}
2324
}
+11
+1-5
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
11
package com.baeldung.scala.exceptionhandling
22

3-
import com.baeldung.scala.exceptionhandling.LegacyErrors.{
4-
ResourceNotFound,
5-
ServerError,
6-
UserNotFound
7-
}
3+
import com.baeldung.scala.exceptionhandling.LegacyErrors._
84

95
import scala.util.{Failure, Success, Try}
106

+1-5
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,7 @@ package com.baeldung.scala.exceptionhandling
33
import cats.MonadError
44
import cats.syntax.flatMap._
55
import cats.syntax.functor._
6-
import com.baeldung.scala.exceptionhandling.LegacyErrors.{
7-
ResourceNotFound,
8-
ServerError,
9-
UserNotFound
10-
}
6+
import com.baeldung.scala.exceptionhandling.LegacyErrors._
117

128
import scala.util.{Failure, Success, Try}
139

+1-4
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,7 @@ package com.baeldung.scala.exceptionhandling
33
import cats.data.ValidatedNel
44
import cats.syntax.apply._
55
import cats.syntax.validated._
6-
import com.baeldung.scala.exceptionhandling.ValidationErrors.{
7-
IllegalLogin,
8-
IllegalPassword
9-
}
6+
import com.baeldung.scala.exceptionhandling.ValidationErrors._
107

118
object HandlingWithValidated {
129
type InvalidOr[T] = ValidatedNel[ValidationErrors, T]
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package com.baeldung.scala.uniontypes
22

3+
import scala.language.implicitConversions
4+
35
object ArbitraryArityUnionType extends App {
46

57
def isIntOrStringOrBool[T: IntOrStringOrBool](t: T): String = t match {
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,8 @@ package com.baeldung.scala.exceptionhandling
22

33
import cats.data.NonEmptyList
44
import cats.data.Validated.{Invalid, Valid}
5-
import com.baeldung.scala.exceptionhandling.LegacyErrors.{
6-
ResourceNotFound,
7-
ServerError,
8-
UserNotFound
9-
}
10-
import com.baeldung.scala.exceptionhandling.ValidationErrors.{
11-
IllegalLogin,
12-
IllegalPassword
13-
}
5+
import com.baeldung.scala.exceptionhandling.LegacyErrors._
6+
import com.baeldung.scala.exceptionhandling.ValidationErrors._
147
import org.scalatest.GivenWhenThen
158
import org.scalatest.featurespec.AnyFeatureSpec
169
import org.scalatest.matchers.should.Matchers

0 commit comments

Comments
 (0)