Created
October 29, 2022 01:37
-
-
Save frgomes/fa6ecbefb16ce654e9c2793f817eac1d to your computer and use it in GitHub Desktop.
Scala - obtain field names and field types from case class employing TypeTag
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import scala.reflect.runtime.universe.TypeTag | |
implicit class ProductExtension[T <: Product : TypeTag](o: T) { | |
def productElementNames: Iterator[String] = { | |
import scala.reflect.runtime.universe._ | |
typeOf[T].members | |
.collect { case m: MethodSymbol if m.isCaseAccessor => m.name.toString } | |
.toList.reverse.toIterator | |
} | |
def productElementTypeNames: Iterator[String] = { | |
import scala.reflect.runtime.universe._ | |
typeOf[T].members | |
.collect { case m: MethodSymbol if m.isCaseAccessor => m.returnType.toString } | |
.toList.reverse.toIterator | |
} | |
} |
Author
frgomes
commented
Oct 29, 2022
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment