Reputation: 7605
I have a case class which has got two fields which are a Seq[Objects]
. These two objects belong to two different case classes.
case class Passport(country: String, name: String)
case class DrivingLicence(code: String, name: String)
case class Docs(passports : Seq[Passport], driv: Seq[DrivingLicence])
val pass1 = Passport("UK", "Michael")
val pass2 = Passport("USA", "Michael")
val driv1 = DrivingLicence("A2", "Mich")
val driv2 = DrivingLicence("A4", "Mich")
val docs = Docs(Seq(pass1,pass2), Seq(driv1,driv2))
I can generate a map with all the attributes and their types like follows:
val r = currentMirror.reflect(docs)
val mapType = r.symbol.typeSignature.members.toStream
.collect{case s : TermSymbol if !s.isMethod => r.reflectField(s)}
.map(r => r.symbol.name.toString.trim -> r.symbol.typeSignature)
.toMap
println("Map of Types: "+mapType)
And the output is:
Map of Types: Map(driv -> Seq[DrivingLicence], passports -> Seq[Passport])
But what I would like is to get rid of the Seq part, and obtain the inner type. Something like:
Map of Types: Map(driv -> DrivingLicence, passports -> Passport)
How could I access the inner type of a Seq[_]
?
Upvotes: 3
Views: 110
Reputation: 22595
You can get a list of type arguments with typeArgs
, but it will return List[Type]
, so you would need to take the first element with head
:
r.symbol.typeSignature.typeArgs.head
Upvotes: 6