As Juh_ suggests, extending a case class should do what you want:
scala> case class E(id: Int, name: String)
defined class E
scala> val e = new E(5, "Prashant")
e: E = E(5,Prashant)
scala> e.name
res3: String = Prashant
Case classes provide an equals method, and they also extend the Product
trait, which is the same trait that Scala tuples extend. Maybe in the future they will also extend the ProductN
traits.
If you use anonymous classes as suggested in some other answers, you don t end up with real tuples! For example, you don t get the equals method:
scala> val x = new { val count = 5 }
x: AnyRef{val count: Int} = $anon$1@29ca901e
scala> val y = new { val count = 5 }
y: AnyRef{val count: Int} = $anon$1@1dfe2924
scala> x == y
res4: Boolean = false
As of Scala 2.11 extending Tuple2
does work, but this is deprecated because you re not supposed to extend case classes.
You could also extend the Product2
trait, but that doesn t provide implementations of any methods, so you d have to write all the methods yourself.
You could probably also use a Shapeless HList
, which would give you lots of fancy features at the cost of adding an external dependency.
I also tried Twitter s jaqen library, but that didn t compile for me in Scala 2.11.
I m currently using Scala 2.11, so I can t guarantee that this advice applies to other versions of Scala.