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 6 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
48 changes: 48 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,54 @@ 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 case class definition in a compatible manner
sideeffffect marked this conversation as resolved.
Show resolved Hide resolved

Sometimes it is desirable to be able to change the definition of a case class (adding and/or removing fields) while still staying compatible with the users of the case class, i.e. not breaking the so called _binary compatibility_.
sideeffffect marked this conversation as resolved.
Show resolved Hide resolved

To achieve that, follow this pattern:
* make the constructor `private`
* define `private` `unapply` function in the companion object (note that by doing that the case class looses the ability to be used in a pattern match)
sideeffffect marked this conversation as resolved.
Show resolved Hide resolved
* define `withXXX` methods on the case class that create a new instance with the respective field changed
sideeffffect marked this conversation as resolved.
Show resolved Hide resolved
* define custom `apply` factory method(s) in the companion object (these can use the private constructor)

Example:

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

```scala
case class Person private (name: String, age: Int):
def withName(name: String) = copy(name = name)
sideeffffect marked this conversation as resolved.
Show resolved Hide resolved
def withAge(age: Int) = copy(age = age)
sideeffffect marked this conversation as resolved.
Show resolved Hide resolved
object Person:
def apply(name: String, age: Int) = new Person(name, age)
private def unapply(p: Person) = p
```
{% endtab %}
{% endtabs %}

sideeffffect marked this conversation as resolved.
Show resolved Hide resolved
Later in time, you can ammend the original case class definition. You
sideeffffect marked this conversation as resolved.
Show resolved Hide resolved
* add a new field `address`,
* add a custom `withAddress` method and
* add an `apply` factory method to the companion.

{% tabs case_class_compat_2 %}
{% tab 'Scala 3 Only' %}
```scala
case class Person private (name: String, age: Int, address: String = ""):
sideeffffect marked this conversation as resolved.
Show resolved Hide resolved
...
def withAddress(address: String) = copy(address = address)
object Person:
...
def apply(name: String, age: Int, address: String) = new Person(name, age, address)
Copy link
Contributor

Choose a reason for hiding this comment

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

I think you should also say that the previous apply method needs to be adapted:

- def apply(name: String, age: Int) = new Person(name, age)
+ def apply(name: String, age: Int) = new Person(name, age, None)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It doesn't, because we manually re-introduce the constructor with the original fields.

```
julienrf marked this conversation as resolved.
Show resolved Hide resolved
{% endtab %}
{% endtabs %}

sideeffffect marked this conversation as resolved.
Show resolved Hide resolved
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 users is maintained.
sideeffffect marked this conversation as resolved.
Show resolved Hide resolved

sideeffffect marked this conversation as resolved.
Show resolved Hide resolved
A regular case class not following this pattern would break its users, because by adding a new field some methods (which could be used by somebody else) change, for example `copy` or the constructor itself.
Copy link
Member

Choose a reason for hiding this comment

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

"would break its users" sounds a bit strange, don't have an alternative yet though.

Copy link
Contributor

Choose a reason for hiding this comment

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

"would brake usage" or "would break compatible usage" or similar perhaps?


## 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