I would like to have an object type that conceptually comprises three pieces of information:
- a string
- a dictionary (Scala map) mapping strings to strings
- a type that can be either "word" or "punctuation"
As much as it matters, this object type is supposed to be the basis for holding the contents of a text document - by having an iterable of instances of it.
I am currently implementing this as a class hierarchy where there is a first class that includes 1 and 2, and then two classes inheriting it, one called "word", the other called "punctuation", as seen here:
abstract class TextElement(initContent: String, initStyleMap: Map[String, String]) {
val content : String = initContent
val styleMap: Map[String, String] = initStyleMap
}
case class Word(initContent: String, initStyleMap: Map[String, String]) extends TextElement(initContent: String, initStyleMap: Map[String, String]) {
}
case class Punctuation(initContent: String, initStyleMap: Map[String, String]) extends TextElement(initContent: String, initStyleMap: Map[String, String]) {
}
I wonder if there's anything more suitable for this than my first intuition just described, as maybe there's leaner or cleaner ways in Scala.
Notably I'm going to have thousands of instances of these objects, so memory footprint is important, as well as quick access to the data. My naive implementation feels a bit boilerplate-ish, and again, maybe there's other approaches I have overlooked.
So I wonder whether there's a viable different approach than this intuitive one, that may have any benefits. Also whether this above code can be made a bit more elegant/concise.