I was just googling to find out how to create a case class with private constructor. Below is the correct way for doing this as described in
How to override apply in a case class companion
object A {
def apply(s: String, i: Int): A =
new A(s.toUpperCase, i) {} //abstract class implementation intentionally empty
}
abstract case class A private[A] (s: String, i: Int) {
private def readResolve(): Object = //to ensure validation and possible singleton-ness, must override readResolve to use explicit companion object apply method
A.apply(s, i)
def copy(s: String = s, i: Int = i): A =
A.apply(s, i)
}
Below is my understanding so far :-
If we declare a case class abstract, then implementation for copy and apply method will not be generated by the compiler.
Below is the question, that I am struggling with :-
Why it is required to provide implementation of readResolve ?