Getting the type for a case class field #15148
Answered
by
adamw
adamw
asked this question in
Metaprogramming
-
I'm trying to get the val fieldTypeRepr = field.tree match
case v: ValDef => v.tpt.tpe
case d: DefDef => d.returnTpt.tpe What would be the correct way to do this? |
Beta Was this translation helpful? Give feedback.
Answered by
adamw
May 9, 2022
Replies: 1 comment 3 replies
-
Assuming that tpe.typeSymbol.caseFields.map { fieldSymbol =>
val fieldTpe = tpe.memberType(fieldSymbol)
...
} |
Beta Was this translation helpful? Give feedback.
3 replies
Answer selected by
adamw
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Assuming that
tpe
is theTypeRepr
for the case class, field symbols can be accessed usingtpe.typeSymbol.caseFields
. Then, to obtain theTypeRepr
for a field, you can use.memberType
, as follows: