score:9
Accepted answer
You can use typeSignatureIn
to get the type signature of a method given GroupResult[Int]
:
import scala.language.experimental.macros
import scala.reflect.macros.Context
case class GroupResult[T](group: String, reduction: Seq[T])
def foo[T] = macro fooImpl[T]
def fooImpl[T: c.WeakTypeTag](c: Context) = {
import c.universe._
val tpe = weakTypeOf[T]
tpe.declarations.collect {
case m: MethodSymbol if m.isCaseAccessor => println(m.typeSignatureIn(tpe))
}
c.literalUnit
}
And then:
scala> foo[GroupResult[Int]]
=> String
=> Seq[Int]
So we're closer, but now we're getting the "types" of the accessors, not their return types. If we want the return types we can use the NullaryMethodType
extractor:
def foo[T] = macro fooImpl[T]
def fooImpl[T: c.WeakTypeTag](c: Context) = {
import c.universe._
val tpe = weakTypeOf[T]
tpe.declarations.collect {
case m: MethodSymbol if m.isCaseAccessor => m.typeSignatureIn(tpe) match {
case NullaryMethodType(returnType) => println(returnType)
}
}
c.literalUnit
}
And then:
scala> foo[GroupResult[Int]]
String
Seq[Int]
And we're done.
Source: stackoverflow.com
Related Query
- scala macro generic field of generic class not apply class generic type parameter
- Scala error: type arguments do not conform to class type parameter bounds
- Scala type inference not working with generic case class and lambda
- scala class extend a trait with generic which is a type of a field
- How to partially apply case class with type parameter in Scala
- Scala type parameterization, Shapeless - could not find implicit value for parameter Generic
- Scala value id is not a member of type parameter Field
- Scala Generic Type as Class Parameter and matching (& Slick)
- Scala macro expansion of class with companion: type not found
- apply modulus operator to Scala generic type class member?
- Apply multiple annotation to a class and/or field using a single type alias in Scala
- Scala type class to extend generic type: No implicits found for parameter
- Scala 2 macro type class derivation for `Coder[P <: Product]` ends with error `P does not take parameters`
- Java Generic Type Converted to Scala does not accept super class itself
- scala error: type arguments do not conform to class type parameter bounds [X >: A,Y >: A]
- Get companion object of class by given generic type Scala
- Scala - obtaining a class object from a generic type
- Compiler error about class graph being not finitary due to a expansively recursive type parameter
- How to create a Scala class with private field with public getter, and primary constructor taking a parameter of the same name
- Scala doobie fragment with generic type parameter
- scala 2.10.2 calling a 'macro method' with generic type not work
- Scala copy case class with generic type
- Scala Generic Function Values (Anonymous Function) - Missing Parameter Type (Error)
- Scala getting field and type of field of a case class
- In Scala Reflection, How to get generic type parameter of a concrete subclass?
- Understanding “inferred type arguments do not conform to type parameter bounds” errors in Scala
- What is the colon in the type parameter of a scala class
- Why is the parameter type of a generic function not inferred?
- Scala trait runtime class from type parameter
- generic trait taking a class and it's companion object as a type parameter
More Query from same tag
- Check if two adjoined elements are ordered using reduceLeft
- flink How to deserialize trait to case
- Conditionally map through rows in CSV file in Scala / Spark to produce another CSV file
- Is the Scala List's cons-operator "::" thread-safe?
- Spark Scala How to use replace function in RDD
- "With" statement equivalent for Scala?
- Logging to a file with Scalatra and Logback
- Scala implicit conversion blocked somehow
- Nothing is being printed out from a Flink Patterned Stream
- Publish single 3rd party jar to local repository in sbt
- How to use DataFrames within SparkListener?
- scalaTest:method run in trait WordSpecLike of type org.scalatest.Status needs `abstract override' modifiers
- What is a good way to copy and mutate list items in Scala?
- Iterate sequence of future in sequence of futures
- Cast a variable to runtime type from existential typed TypeCast in Scala
- Scala for Android - Curious type mismatch error
- Typesafe RPC shared between client/server but with REST methods
- Stringbuilder to RDD
- Designing a filter using scala - For loop unrolling
- java.lang.ClassNotFoundException: org.apache.spark.sql.sources.v2.DataSourceV2 for Spark 3.0.0
- How to drop a row in a JSON file with invalid format
- How to make Slick respect DB column defaults on insert?
- Embedded JSON Objects not saved in BsonRecordListField using fromJSON
- How to fix the pattern-matching exhaustive warning?
- Strange type of scala "=>:[_, _]"
- Scala Implicit function within a function call
- Why can't I specify a result type for an anonymous function in Scala?
- Play Scala Json Missing Property vs Null
- Play Framework / Twirl Dynamically include templates
- Why do you need Arbitraries in scalacheck?