You can use objects that inherit from Product . It will be easier and safer if you know arity in advance:
def getMax(foos: List[Product2[Int,Int]], f: Product2[Int,Int] => Int) = foos.map{f} ....
Then you can give getMax something like Tuple , for example.
class Foo(val prop1: Int, val prop2: Int) extends Tuple2[Int, Int](prop1, prop2) // this will duplicate values in an object actually. getMax((new Foo(1,2)), _._2)
or inherit the right from Product :
class Bar(val prop1: Int, val prop2: Int) extends Product2[Int, Int] { def _1 = prop1 def _2 = prop2 } val b = new Bar(2, 3) getMax(List(b), _._2)
or just use Scala tuples:
getMax( (1,10) :: Nil, _._2) getMax( List(1 -> 10), _._2)
Everything will become more complicated if you do not know arity in advance, because the general Product will allow you to retrieve elements only as Any (see the Product.productElement(n: Int) method) - this way you lose type safety.
source share