You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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:
Later in time, you can amend the original case class definition to, say, add an optional `address` field. You
221
238
* add a new field `address` and a custom `withAddress` method,
222
-
*add the former constructor signature as a secondary constructor, private to the companion object. This step is necessary because the compilers currently emit the private constructors as public constructors in the bytecode (see [#12711](https://github.com/scala/bug/issues/12711) and [#16651](https://github.com/lampepfl/dotty/issues/16651)).
239
+
*tell MiMa to [ignore](https://github.com/lightbend/mima#filtering-binary-incompatibilities) changes to the class constructor. This step is necessary because MiMa does not yet ignore changes in private class constructor signatures (see [#738](https://github.com/lightbend/mima/issues/738)).
> 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:
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”.
263
+
264
+
> Note that an alternative solution, instead of adding a MiMa exclusion filter, consists of adding back the previous
265
+
> constructor signatures as secondary constructors:
>Otherwise, MiMa would fail with an error like “method this(java.lang.String,Int)Unit in classPerson does not have a correspondent in current version”.
272
+
242
273
The original users can use the caseclass`Person`asbefore, all the methods that existed before are present unmodified after this change, thus the compatibility with the existing usage is maintained.
243
274
244
275
Thenew field `address` can be used asfollows:
245
276
277
+
{% tabs case_class_compat_6 %}
278
+
{% tab 'Scala2 and 3' %}
246
279
~~~ scala
247
280
// The public constructor sets the address to None by default.
A regular caseclassnot 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.
0 commit comments