I am porting my java code to scala, and have a constructor that does quite some stuff:
- open hibernate session to load data
- do loading (now the session is kept open! I need lazy loading further on)
- perform some operations on loaded data and put in containers
- tie containers with their consumers
- ..blah blah
- close session
The constructor produces quite a number (~20) of object members, that are final (val in scala). Their creation is not independent, also note that session. Obviously I don't want the session and other temporary stuff to become constructed instance members. How do I do that in scala?
Similar questions have been already asked before:
Answers given in previous topics suggest either returning a tuple of ~20 fields from initialization method, or creating a private constructor that takes those ~20 values as parameters from companion object apply method. Either seems pig ugly to me.
So far the most sensible approach seems to declare those fields as vars, not vals, and reassign them in some initialization method, which also kinda smells.