I have a class which represents posts in our system. Where a post might represent a question, document, image, etc. There are about 7 different types of objects the Post class can represent. Each of the 7 different types of objects we have has it's own metadata class to store additional object specific information.
Currently my Post class has 7 optional attributes, one of which gets filled depending on the type of object it is. But since the Post class will only ever have one of these 7 attributes filled, is there a way to consolidate these into a single attribute with an arbitrary type? Then I could use a match case statement to generate the correct metadata object at runtime. Or this impossible with Scala given the strongly typed nature of the language.
Code is below:
case class Post ( id : Long, typ : String, name : String, fileInfo : Option[FileInfo], imageInfo : Option[FileImageInfo], videoInfo : Option[FileVideoInfo], audioInfo : Option[FileAudioInfo], eventInfo: Option[EventInfo], lectureInfo: Option[LectureInfo], drawingInfo: Option[DrawingInfo] ) object Post { val simple = { get[Long]("object_view.id") ~ get[String]("object_view.type") ~ get[String]("object_view.name") map { case id~typ~name => Post( id, typ, name, FileInfo.getById(id), FileImageInfo.getById(id), FileVideoInfo.getById(id), FileAudioInfo.getById(id), EventInfo.getFirst(id), LectureInfo.getById(id), DrawingInfo.getById(id) ) } }
trait Post { def id: Long def name: String } case class FilePost(id:Long, name:String, info: FileInfo) extends Post { }