Scala concepts: Class Inside Def

Like Def Inside Def, classes can also be defined in defs.

This is particularly useful when defining temporary data types, in particular groupings.

type Language = String

type Color = String

type Translation = String

def generateColors: Map[(Language, Color), Translation] = {
  final case class ColorMapping(red: String, green: String, blue: String) {
    def toMap: Map[String, String] =
      Map("red" -> red, "green" -> green, "blue" -> blue)
  }
  val translations = Map(
    "Spanish" -> ColorMapping("rojo", "verde", "azul"),
    "French" -> ColorMapping("rouge", "vert", "bleu"),
    "Chinese" -> ColorMapping("红", "绿", "蓝")
  )

  for {
    (language, mapping) <- translations
    (color, translation) <- mapping.toMap
  } yield (language, color) -> translation
}