Let's say I have a class that looks something like this:
class Foo(Prop1:Int, Prop2:Int, Prop3:Int)
{
..
}
And I wanted to create a function that gets the max of some arbitrary property from a list of Foo
s.
Like this:
def getMax(Foos:List[Foo], Property:??) = Foos.map(_.Property).sort(_ > _).head
If I called getMax(myFooList, Prop1)
, it would return the value of the highest Prop1
from this list of Foo
s.
My question is, how can I make this work? I guess I could create some kind of enum (the scala equivalent) for the Property
and do a match
and then run the map
on the appropriate property, but that seems like a lot of work - I'd have to extend my enum and the function each time Foo
is refactored.
Also, not as important, but is there a better way to grab the max value of a list then what I did?