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

Sequences with varargs type ascriptions represented unintuitively in macros #6777

Closed
scabug opened this issue Dec 6, 2012 · 2 comments
Closed

Comments

@scabug
Copy link

scabug commented Dec 6, 2012

When foo is given a sequence with a varargs type ascription in the following code, ys isn't actually a sequence of integer expressions—it's more like a c.Expr[Seq[Int]].

object MacroExample {
  def foo(xs: Int*): Any = macro foo_impl
  def foo_impl(c: Context)(ys: c.Expr[Int]*) = ???
}

Given the following, for example:

import language.experimental.macros
import scala.reflect.macros.Context

object MacroExample {
  def foo(xs: Int*): Any = macro foo_impl
  def foo_impl(c: Context)(xs: c.Expr[Int]*) = xs.head

  def bar(xs: Int*): Any = macro bar_impl
  def bar_impl(c: Context)(xs: c.Expr[Int]*) = {
    import c.universe._

    xs.head.tree match {
      case Typed(e, _) => c.Expr(e)
      case i => c.Expr(i)
    }
  }
}

We get an unhelpful error message when calling foo with a type-ascripted sequence:

scala> MacroExample.foo(List(1, 2, 3): _*)
<console>:8: error: no `: _*' annotation allowed here
(such annotations are only allowed in arguments to *-parameters)
              MacroExample.foo(List(1, 2, 3): _*)
                                            ^

And even stranger behavior with bar:

scala> MacroExample.bar(List(1, 2, 3): _*)
res0: List[Int] = List(1, 2, 3)

scala> MacroExample.bar(1, 2, 3)
res1: Int = 1

See this Stack Overflow question and this example for more discussion.

@scabug
Copy link
Author

scabug commented Dec 6, 2012

Imported From: https://issues.scala-lang.org/browse/SI-6777?orig=1
Reporter: Travis Brown (travisbrown)
Affected Versions: 2.10.0-RC3

@SethTisue
Copy link
Member

closing stale enhancement tickets, especially in areas like scala.reflect which are mostly dormant

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

3 participants