Skip to content

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

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

Merged
merged 26 commits into from
Jan 13, 2023
Merged
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Update _overviews/tutorials/binary-compatibility-for-library-authors.md
Co-authored-by: Jakub Kozłowski <kubukoz@gmail.com>
  • Loading branch information
sideeffffect and kubukoz authored Dec 21, 2022
commit 1118fb3e47a5ed698118ca0f7032770844f830c8
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ Sometimes it is desirable to be able to change the definition of a case class (a

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)
* define a private `unapply` function in the companion object (note that by doing that the case class loses the ability to be used in a pattern match)
Copy link
Contributor

@bjornregnell bjornregnell Dec 22, 2022

Choose a reason for hiding this comment

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

Well, usage of instances in patterns is possible e.g. typed patterns such as
case p: Person =>
so it is "only" constructor patterns and extractor patterns that are not possible, if I'm correct?

So case Person(n,a) => would not work as unapply is private, but other kind of patterns work. So I think this should be clarified as the reader might not know about all the unapply mechanics under the hood...

The terminology of all the 15, or so, different kinds of patterns is available here: https://scala-lang.org/files/archive/spec/2.13/08-pattern-matching.html

Copy link
Contributor

Choose a reason for hiding this comment

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

Edit: I guess extractor patterns could be made to work if the library author provides custom extractors?

Copy link
Contributor

Choose a reason for hiding this comment

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

Anyway, I think it would be good to explain the implications on pattern matching in more detail...

* define `withXXX` methods on the case class that create a new instance with the respective field changed
* define custom `apply` factory method(s) in the companion object (these can use the private constructor)

Expand Down