For example (maybe a little clumsy from real life, but just for illustration):
βUserβ is the case class containing the username and identifier. An identifier can never be set manually, and an instance of a user class without a set of identifiers does not make sense.
The UserBase class supports the user base and has a getUser (name: String): User method that returns a sequential instance of the user.
No one except the UserBase object can know (well, someone can, but really should not rely on this knowledge) the user ID, so creating a user instance manually does not make sense (and can lead to errors in the future if someone hardcodes accidentally forgets this). In addition, an unwanted instance of an orphaned user that is not tracked by UserBase is also undesirable.
So the task is to make calling UserBase.getUser the only way to get the user instance.
Could this be implemented in Scala?
source
share