If I want a serialization-safe singleton, should I prefer
case object Foo
or
object Foo extends Serializable
?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
I think this depends on how you plan to use this object. Case objects are generally used with case classes to represent some kind of initial or terminal object in an algebraic data type, eg
NilorNone. Regular objects usually are companions for classes to hold static methods like singleton and factory methods.If you’re planning using this object with other classes, serializing it, and maybe using it in pattern matching, defining it as a case object seems more natural to me.