group by list on same field with different case classes scala -
how can grouped 2 list contains different case classes, on both classes have same field:
case class x(a:long, b:long, c:long, d:long) case class y(a:long, e:long, f :long) val = list(x(10,10,8,8)) val j = list(y(10,10,8)) val joined = a++b joined.groupby(_.a)
error: value not member of product serializable
thanks
fearing not pattern match:
scala> val joined = (i++j) groupby { case x: x => x.a case y: y => y.a } joined: scala.collection.immutable.map[long,list[product serializable]] = map(10 -> list(x(10,10,8,8), y(10,10,8)))
crudely requested:
scala> val joined = ((i++j).asinstanceof[list[{ def a: long }]]) groupby(_.a) warning: there 1 feature warning(s); re-run -feature details joined: scala.collection.immutable.map[long,list[anyref{def a: long}]] = map(10 -> list(x(10,10,8,8), y(10,10,8)))
or
scala> val joined = (i++j) groupby { case x => x.asinstanceof[{ def a: long }].a } warning: there 1 feature warning(s); re-run -feature details joined: scala.collection.immutable.map[long,list[product serializable]] = map(10 -> list(x(10,10,8,8), y(10,10,8)))
is resulting collection well-typed?
update:
(i++j) groupby (_.asinstanceof[{ def a: long }].a)
i wonder if ide has refactor, "convert underscore"?
Comments
Post a Comment