Skip to content

Instantly share code, notes, and snippets.

@smithjessk
Last active August 1, 2016 18:35
Show Gist options
  • Save smithjessk/92d0384e3e8fdae355c1c87f2b1b309d to your computer and use it in GitHub Desktop.
Save smithjessk/92d0384e3e8fdae355c1c87f2b1b309d to your computer and use it in GitHub Desktop.
Demonstrates a problem with by-name parameter extraction
// Inspired by http://stackoverflow.com/questions/29757584/handling-by-name-parameters-in-scala-macro
import scala.reflect.runtime.universe._
class X { def x(i: => Int, second: String, other: Int*) = i * 2 }
val typeSignature = typeOf[X].member(TermName("x")).typeSignature
val paramTypes = typeSignature match {
case MethodType(params, _) => params map { _.typeSignature }
}
// Need to be assigned here because they are voltaile
val javaRepeatedParamDefinition = definitions.JavaRepeatedParamClass
val repeatedParamDefinition = definitions.RepeatedParamClass
val byNameDefinition = definitions.ByNameParamClass
// `signature` for the first and third parameters matches on whichever one of these cases is first.
val byNameParams = paramTypes map { signature =>
signature match {
case TypeRef(_, javaRepeatedParamDefinition, args) =>
println("Got JavaRepeatedParamClass")
args
case TypeRef(_, repeatedParamDefinition, args) =>
println("Got repeatedParamDefinition")
args
case TypeRef(_, byNameDefinition, args) =>
println("Got byNameDefinition")
args
}
} // byNameParams = List(List(scala.Int), List(), List(scala.Int))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment