Skip to content
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

Document the pattern for evolving case classes in a compatible manner #2662

Merged
merged 26 commits into from
Jan 13, 2023
Merged
Changes from 21 commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
e54f7f1
Document the patter for evolving case classes in a compatible manner
sideeffffect Dec 21, 2022
5c8df68
Update domain-modeling-tools.md
sideeffffect Dec 21, 2022
0361062
Update binary-compatibility-for-library-authors.md
sideeffffect Dec 21, 2022
3b1fc57
Update binary-compatibility-for-library-authors.md
sideeffffect Dec 21, 2022
6b8b157
Update domain-modeling-tools.md
sideeffffect Dec 21, 2022
75b5937
Update binary-compatibility-for-library-authors.md
sideeffffect Dec 21, 2022
1118fb3
Update _overviews/tutorials/binary-compatibility-for-library-authors.md
sideeffffect Dec 21, 2022
64776be
Update binary-compatibility-for-library-authors.md
sideeffffect Dec 21, 2022
b583d46
Update binary-compatibility-for-library-authors.md
sideeffffect Dec 22, 2022
a6d4fc0
Update _overviews/tutorials/binary-compatibility-for-library-authors.md
sideeffffect Dec 22, 2022
85bf8e7
Update _overviews/tutorials/binary-compatibility-for-library-authors.md
sideeffffect Dec 22, 2022
359219e
Update _overviews/tutorials/binary-compatibility-for-library-authors.md
sideeffffect Dec 22, 2022
25af00f
Update _overviews/tutorials/binary-compatibility-for-library-authors.md
sideeffffect Dec 22, 2022
b328958
Update _overviews/tutorials/binary-compatibility-for-library-authors.md
sideeffffect Dec 22, 2022
caaa532
Update _overviews/tutorials/binary-compatibility-for-library-authors.md
sideeffffect Dec 22, 2022
9afdd84
Update _overviews/tutorials/binary-compatibility-for-library-authors.md
sideeffffect Dec 22, 2022
7806dbd
Update _overviews/tutorials/binary-compatibility-for-library-authors.md
sideeffffect Dec 22, 2022
a764a9c
Update binary-compatibility-for-library-authors.md
sideeffffect Dec 22, 2022
a52d7d6
Update binary-compatibility-for-library-authors.md
sideeffffect Dec 22, 2022
7668763
Apply suggestions from code review
sideeffffect Jan 1, 2023
293a0bd
Update binary-compatibility-for-library-authors.md
sideeffffect Jan 1, 2023
5c1d9b3
Update _overviews/tutorials/binary-compatibility-for-library-authors.md
sideeffffect Jan 10, 2023
4f548e9
Apply suggestions from code review
sideeffffect Jan 10, 2023
f3e6c15
Revert "Apply suggestions from code review"
julienrf Jan 13, 2023
3ab5b4b
Final adjustments
julienrf Jan 13, 2023
a818702
Actual final adjustments
julienrf Jan 13, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 79 additions & 0 deletions _overviews/tutorials/binary-compatibility-for-library-authors.md
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,85 @@ You can find detailed explanations, runnable examples and tips to maintain binar

Again, we recommend using MiMa to double-check that you have not broken binary compatibility after making changes.

### Changing a case class definition in a backwards-compatible manner

Sometimes, it is desirable to change the definition of a case class (adding and/or removing fields) while still staying backwards-compatible with the existing usage of the case class, i.e. not breaking the so-called _binary compatibility_. The first question you should ask yourself is “do you need a _case_ class?” (as opposed to a regular class, which can be easier to evolve in a binary compatible way). A good reason for using a case class is when you need a structural implementation of `equals` and `hashCode`.

To achieve that, follow this pattern:
* make the primary constructor private (this makes private the `copy` method of the class)
* define a private `unapply` function in the companion object (note that by doing that the case class loses the ability to be used as an extractor in match expressions)
* for all the fields, define `withXXX` methods on the case class that create a new instance with the respective field changed (you can use the private `copy` method to implement them)
* create a public constructor by defining an `apply` method in the companion object (it can use the private constructor)

Example:

{% tabs case_class_compat_1 %}
{% tab 'Scala 3 Only' %}

```scala
// Mark the primary constructor as private
case class Person private (name: String, age: Int):
// Create withXxx methods for every field, implemented by using the copy method
def withName(name: String): Person = copy(name = name)
def withAge(age: Int): Person = copy(age = age)
object Person:
// Create a public constructor (which uses the primary constructor)
def apply(name: String, age: Int) = new Person(name, age)
// Make the extractor private
private def unapply(p: Person) = p
```
{% endtab %}
{% endtabs %}
sideeffffect marked this conversation as resolved.
Show resolved Hide resolved
This class can be published in a library and used as follows:

sideeffffect marked this conversation as resolved.
Show resolved Hide resolved
~~~ scala
// Create a new instance
val alice = Person("Alice", 42)
// Transform an instance
println(alice.withAge(alice.age + 1)) // Person(Alice, 43)
~~~
sideeffffect marked this conversation as resolved.
Show resolved Hide resolved

If you try to use `Person` as an extractor in a match expression, it will fail with a message like “method unapply cannot be accessed as a member of Person.type”. Instead, you can use it as a typed pattern:

~~~ scala
alice match
case person: Person => person.name
~~~
sideeffffect marked this conversation as resolved.
Show resolved Hide resolved
Later in time, you can amend the original case class definition to, say, add an optional `address` field. You
* add a new field `address` and a custom `withAddress` method,
* add the former constructor signature as a secondary constructor, private to the companion object. This step is necessary because the public `apply` method in the companion object calls the former constructor, which was effectively public in the bytecode produced by the compiler.
sideeffffect marked this conversation as resolved.
Show resolved Hide resolved

{% tabs case_class_compat_2 %}
{% tab 'Scala 3 Only' %}
```scala
case class Person private (name: String, age: Int, address: Option[String]):
...
// Add back the former primary constructor signature
private[Person] def this(name: String, age: Int) = this(name, age, None)
def withAddress(address: Option[String]) = copy(address = address)
```
julienrf marked this conversation as resolved.
Show resolved Hide resolved
{% endtab %}
{% endtabs %}
sideeffffect marked this conversation as resolved.
Show resolved Hide resolved

sideeffffect marked this conversation as resolved.
Show resolved Hide resolved
> Note that an alternative solution, instead of adding back the previous constructor signatures as secondary constructors, consists of adding a [MiMa filter](https://github.com/lightbend/mima#filtering-binary-incompatibilities) to simply ignore the problem. Even though the constructors are effectively public in the bytecode, they can’t be called from Scala programs (but they could be called by Java programs). In an sbt build definition you would add the following setting:
> ~~~ scala
> import com.typesafe.tools.mima.core._
> mimaBinaryIssueFilters += ProblemFilters.exclude[DirectMissingMethodProblem]("Person.this")
> ~~~
> Otherwise, MiMa would fail with an error like “method this(java.lang.String,Int)Unit in class Person does not have a correspondent in current version”.
The original users can use the case class `Person` as before, all the methods that existed before are present unmodified after this change, thus the compatibility with the existing usage is maintained.

sideeffffect marked this conversation as resolved.
Show resolved Hide resolved
The new field `address` can be used as follows:

~~~ scala
// The public constructor sets the address to None by default.
// To set the address, we call withAddress:
val bob = Person("Bob", 21).withAddress(Some("Atlantic ocean"))
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@julienrf Thanks for you suggestions. I've applied them all. But I think this part is worth a discussion.

Shouldn't we recommend readers to also include the apply methods in the companion object? Because

Person("Bob", 21).withAddress(Some("Atlantic ocean"))

looks a bit clumsy. This looks much better

Person("Bob", 21, "Atlantic ocean")

I know, we save on some boilerplate in the class definition. But we're just pushing the boilerplate onto the users of the class. Is that the right choice to make?

Ideally we'd have our cake and eat it too, but I don't know if it's possible without a new language level feature...

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don’t think the pattern I suggested is clumsy. It is similar to the “builder” pattern we often see around.

Anyway, there are probably several possible variations. The one I suggest here provides a public-facing API that (I think) is simpler because it contains fewer overloads of the apply methods.

Copy link
Contributor

@bjornregnell bjornregnell Jan 3, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, documentation space is not scarce :) so I suggest to explain both possibilities here: withX or overloaded apply constructors. It's up to the library author. And if the library user is "refused" a more concise constructor then it's just a self-made extension away...

println(bob.address)
~~~
sideeffffect marked this conversation as resolved.
Show resolved Hide resolved

A regular case class not following this pattern would break its usage, because by adding a new field changes some methods (which could be used by somebody else), for example `copy` or the constructor itself.

## Versioning Scheme - Communicating compatibility breakages

Library authors use versioning schemes to communicate compatibility guarantees between library releases to their users. Versioning schemes like [Semantic Versioning](https://semver.org/) (SemVer) allow
Expand Down