Using split's data in order in a data model

I want to put the result of a split, with the same order into a data model (case class). How to do this without using additional vals. What I mean is this:

Lets say I have a case calss:

case class Result(eachValue: String, ofValue: String, thisValue: String, couldValue: String, beValue: String, aValue: String, valueValue: String)

Then have a String like this:

res0: Array[String] = Array("each", "of", "this", "could", "be", "a", "value")

How I can mapped the result into an instance of the case class?

I don’t think it is possible without assigning names to the array elements. The shortest I could come up with was

"".split('.') match {
  case Array(eachV, ofV, thisV, couldV, beV, aV, valueV, _*) =>
      Result(eachV, ofV, thisV, couldV, beV, aV, valueV)

which silently ignores if there are too many dots in the string and fails with a MatchError if there aren’t enough (if you want both to throw a MatchError, just remove the _* part).

The problem is, that the compiler cannot guarantee the number of elements that will result from the call to split, while the case class requires a fixed amount of parameters.

1 Like

How about:

val values = “”.split(’.’)

val result = Result(values(0), values(1), values(2), values(3), values(4), values(5), values(6))