diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..c125e70 --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +.bsp +.metals +.vscode +target/ \ No newline at end of file diff --git a/.scalafmt.conf b/.scalafmt.conf new file mode 100644 index 0000000..439185c --- /dev/null +++ b/.scalafmt.conf @@ -0,0 +1,2 @@ +version = "3.6.1" +runner.dialect = scala213 \ No newline at end of file diff --git a/app/src/main/scala/App.scala b/app/src/main/scala/App.scala new file mode 100644 index 0000000..e18fd41 --- /dev/null +++ b/app/src/main/scala/App.scala @@ -0,0 +1,8 @@ +import cats.syntax.all._ +import PersonSemigroup._ + +object App { + def main(args: Array[String]): Unit = { + println(Person("foo", 2) |+| Person("bar", 3)) + } +} diff --git a/build.sbt b/build.sbt new file mode 100644 index 0000000..3e41463 --- /dev/null +++ b/build.sbt @@ -0,0 +1,21 @@ +ThisBuild / scalaVersion := "3.2.2" +ThisBuild / crossScalaVersions := Seq("2.13.10", "3.2.2") + +lazy val personOld = project +lazy val personNew = project.settings( + mimaPreviousClassfiles := Map( + projectID.value -> (personOld / Compile / classDirectory).value + ), + mimaReportBinaryIssues := + mimaReportBinaryIssues + .dependsOn(personOld / Compile / compile) + .value +) + +lazy val lib = project + .settings( + libraryDependencies += "org.typelevel" %% "kittens" % "3.0.0" + ) + .dependsOn(personOld % Provided) + +val app = project.dependsOn(lib, personNew) diff --git a/lib/src/main/scala/PersonSemigroup.scala b/lib/src/main/scala/PersonSemigroup.scala new file mode 100644 index 0000000..a8e72b3 --- /dev/null +++ b/lib/src/main/scala/PersonSemigroup.scala @@ -0,0 +1,6 @@ +import cats._ +import cats.derived._ + +object PersonSemigroup { + implicit def instance: Semigroup[Person] = semiauto.semigroup +} diff --git a/personNew/src/main/scala/Person.scala b/personNew/src/main/scala/Person.scala new file mode 100644 index 0000000..646aced --- /dev/null +++ b/personNew/src/main/scala/Person.scala @@ -0,0 +1,17 @@ +case class Person private (name: String, age: Int) { + def this(name: String) = this(name, -1) + + def withName(name: String): Person = copy(name = name) + def withAge(age: Int): Person = copy(age = age) + + private def copy(name: String = this.name, age: Int = this.age): Person = + new Person(name, age) +} + +object Person { + def apply(name: String, age: Int): Person = new Person(name, age) + + def apply(name: String): Person = new Person(name) + + private def unapply(person: Person): Some[Person] = Some(person) +} diff --git a/personOld/src/main/scala/Person.scala b/personOld/src/main/scala/Person.scala new file mode 100644 index 0000000..67d7541 --- /dev/null +++ b/personOld/src/main/scala/Person.scala @@ -0,0 +1,11 @@ +case class Person private (name: String) { + def withName(name: String): Person = copy(name = name) + + private def copy(name: String = this.name): Person = + new Person(name) +} + +object Person { + def apply(name: String): Person = new Person(name) + private def unapply(person: Person): Some[Person] = Some(person) +} diff --git a/project/build.properties b/project/build.properties new file mode 100644 index 0000000..46e43a9 --- /dev/null +++ b/project/build.properties @@ -0,0 +1 @@ +sbt.version=1.8.2 diff --git a/project/plugins.sbt b/project/plugins.sbt new file mode 100644 index 0000000..6984693 --- /dev/null +++ b/project/plugins.sbt @@ -0,0 +1 @@ +addSbtPlugin("com.typesafe" % "sbt-mima-plugin" % "1.1.2")