Last active
August 1, 2016 18:35
-
-
Save smithjessk/92d0384e3e8fdae355c1c87f2b1b309d to your computer and use it in GitHub Desktop.
Demonstrates a problem with by-name parameter extraction
This file contains 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
// 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