Using Generic with a nested case class

677 views
Skip to first unread message

Brendan

unread,
Feb 14, 2014, 7:02:34 AM2/14/14
to shapel...@googlegroups.com
Hi,

I'm trying to convert an HList to a case class with a nested case class within it
I can see that using Generic is only converting the case class to an HList at the top level 

Is there a simple to way to achieve this?

Thanks
Brendan

scala> import shapeless._
import shapeless._

scala> case class Address(street : String, city : String, postcode : String)
defined class Address

scala> case class Person(name : String, age : Int, address : Address)
defined class Person

scala> val personGen = Generic[Person]
personGen: shapeless.Generic[Person]{type Repr = shapeless.::[String,shapeless.::[Int,shapeless.::[Address,shapeless.HNil]]]} = $1$$1@4ac8cdf4

scala> val personHList = "tom" :: 10 :: "downing st" :: "london" :: "sw1" :: HNil
personHList: shapeless.::[String,shapeless.::[Int,shapeless.::[String,shapeless.::[String,shapeless.::[String,shapeless.HNil]]]]] = tom :: 10 :: downing st :: london :: sw1 :: HNil

scala> val person = personGen.from(personHList)
<console>:16: error: type mismatch;
 found   : shapeless.::[String,shapeless.::[Int,shapeless.::[String,shapeless.::[String,shapeless.::[String,shapeless.HNil]]]]]
 required: personGen.Repr
    (which expands to)  shapeless.::[String,shapeless.::[Int,shapeless.::[Address,shapeless.HNil]]]
       val person = personGen.from(personHList)

Miles Sabin

unread,
Apr 9, 2014, 6:57:28 PM4/9/14
to shapel...@googlegroups.com
On Fri, Feb 14, 2014 at 12:02 PM, Brendan <brendan....@gmail.com> wrote:
> I'm trying to convert an HList to a case class with a nested case class
> within it I can see that using Generic is only converting the case class to an HList
> at the top level

Yes, that's right, Generic is designed for traversing/transforming one
layer at a time.

> Is there a simple to way to achieve this?

What is it exactly that you want to do? The everything and everywhere
combinators traverse/transform across multiple layers: you can find a
couple of examples here,

https://github.com/milessabin/shapeless/blob/master/examples/src/main/scala/shapeless/examples/sybclass.scala

Cheers,


Miles

--
Miles Sabin
tel: +44 7813 944 528
skype: milessabin
gtalk: mi...@milessabin.com
g+: http://www.milessabin.com
http://twitter.com/milessabin

brendan....@gmail.com

unread,
Apr 25, 2014, 10:40:43 AM4/25/14
to shapel...@googlegroups.com
I was attempting to be able to take a flat tuple of values and convert it into an arbitrarily nested case class. 
It was specifically going to used to transform a java.sql.ResultSet into any case class (which may contain other case classes)

We ended up being able to implement it in a different way

Thanks for your help
Brendan

Владимир Морозов

unread,
Jun 28, 2015, 6:03:14 PM6/28/15
to shapel...@googlegroups.com
Hi,

I try to solve similar problem, could you share information about how you map ResultSet to nested case classes ?

пятница, 25 апреля 2014 г., 17:40:43 UTC+3 пользователь Brendan Maginnis написал:

Antonio Fijo

unread,
Jun 14, 2017, 6:33:49 AM6/14/17
to Shapeless Dev
Hi all,
I guess this is what is wanted:

case class Address(street : String, city : String, postcode : String)
case class Person(name : String, age : Int, address : Address)

val addressGen = Generic[Address]

val personGen = Generic[Person]

val addressHList = "downing st" :: "london" :: "sw1" :: HNil
val address = addressGen.from(addressHList)

val personHList = "tom" :: 10 :: address :: HNil
val person = personGen.from(personHList)

println(person) // Person(tom,10,Address(downing st,london,sw1))

Miles Sabin

unread,
Jun 14, 2017, 6:36:25 AM6/14/17
to shapel...@googlegroups.com
Hi folks,

This is a reminder that this mailing list is deprecated. Q&A on this
sort is probably best handled on StackOverflow. More general
discussion belongs on the shapeless gitter channel,

https://gitter.im/milessabin/shapeless

Cheers,


Miles
> --
> You received this message because you are subscribed to the Google Groups
> "Shapeless Dev" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to shapeless-de...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.



--
Miles Sabin
tel: +44 7813 944 528
skype: milessabin
gtalk: mi...@milessabin.com
http://milessabin.com/blog
http://twitter.com/milessabin
Reply all
Reply to author
Forward
0 new messages