I guess the short answer is "no".
A slightly longer answer is that, if you want to retrieve a field from any element, using just a string with the name of that element, then you have to use reflection.
That said, you could write a look-up function, as follows:
case class Foo(fieldA: Option[Double], fieldB: Option[Double]) {
//...
def byFieldName(name: String): Option[Double] = name match {
case "fieldA" => fieldA
case "fieldB" => fieldB
}
}
It's a bit manual, but it works. Alternatively, you could implement this function using reflection, caching the result in a Map[String, Option[Double]] (assuming all such fields have the same type). That way, you would only get the performance hit once for each looked-up field.
UPDATE: I should point out that maybe this isn't what you want in any case. If you need to retrieve field values by name, would the following work for you instead of a case class (demonstrated in the Scala REPL)?
scala> type Foo = Map[String, Option[Double]]
defined type alias Foo
scala> val foo: Foo = Map("fieldA" -> Some(1.0), "fieldB" -> Some(2.0))
foo: Foo = Map(fieldA -> Some(1.0), fieldB -> Some(2.0))
scala> foo("fieldA")
res0: Option[Double] = Some(1.0)
That is, could you use a Map data structure to lookup labeled values?
foo.fieldA.getwhich returns1.0.fieldAisSome(1.0)notSome(2.0)."fieldA", I don't really know it'sfoo.fieldAthat I need. So the question is how to getfoo.fieldAfrom the string.