Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pattern q"new $t(..$args)" does not work for zero argument constructors #10138

Closed
scabug opened this issue Jan 7, 2017 · 5 comments
Closed

Comments

@scabug
Copy link

scabug commented Jan 7, 2017

Welcome to Scala 2.12.1 (Java HotSpot(TM) 64-Bit Server VM, Java 1.8.0_102).
Type in expressions for evaluation. Or try :help.

scala> import scala.reflect.runtime.universe._
import scala.reflect.runtime.universe._

scala> val q"new $t(..$arguments)" = q"new Foo(bar, baz)"
t: reflect.runtime.universe.Tree = Foo
arguments: List[reflect.runtime.universe.Tree] = List(bar, baz)

scala> val q"new $t(..$arguments)" = q"new Foo(bar)"
t: reflect.runtime.universe.Tree = Foo
arguments: List[reflect.runtime.universe.Tree] = List(bar)

scala> val q"new $t(..$arguments)" = q"new Foo()"
scala.MatchError: new Foo() (of class scala.reflect.internal.Trees$Apply)
  ... 29 elided
@scabug
Copy link
Author

scabug commented Jan 7, 2017

Imported From: https://issues.scala-lang.org/browse/SI-10138?orig=1
Reporter: @Atry
Affected Versions: 2.12.1

@scabug
Copy link
Author

scabug commented Jan 9, 2017

@densh said:
New and constructors are a bit special with respect to how they treat nullary applications (there is no way to distinguish new Foo and new Foo()). You should use ...$arguments to match cases like these.

@scabug scabug closed this as completed Jan 9, 2017
@scabug
Copy link
Author

scabug commented Jan 13, 2017

@Atry said (edited on Jan 13, 2017 7:00:43 AM UTC):
Did you mean "You should NOT use ...$arguments to match cases like these." ?

@scabug
Copy link
Author

scabug commented Jan 13, 2017

@densh said (edited on Jan 13, 2017 9:59:59 AM UTC):
Sorry if I was unclear, ...$arguments will match any arity of arguments. then you can check that it's an empty list as a pattern matching guard for example.

scala> val q"new $_(...$argss)" = q"new Foo"
argss: List[List[reflect.runtime.universe.Tree]] = List()

scala> val q"new $_(...$argss)" = q"new Foo()"
argss: List[List[reflect.runtime.universe.Tree]] = List()

scala> val q"new $_(...$argss)" = q"new Foo(x)"
argss: List[List[reflect.runtime.universe.Tree]] = List(List(x))

scala> val q"new $_(...$argss)" = q"new Foo(x, y)"
argss: List[List[reflect.runtime.universe.Tree]] = List(List(x, y))

scala> val q"new $_(...$argss)" = q"new Foo(x)(y)"
argss: List[List[reflect.runtime.universe.Tree]] = List(List(x), List(y))

@scabug
Copy link
Author

scabug commented Jan 14, 2017

@som-snytt said:
For those of us with less than stellar vision.

Dots near unquotee annotate degree of flattening and are also called splicing rank. 
..$ expects argument to be an Iterable[Tree] and ...$ expects Iterable[Iterable[Tree]].

Also glossary.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants