Skip to content

scala.reflect.macros: the importance of 'c' #10615

Open
@Andrei-Pozolotin

Description

@Andrei-Pozolotin

following example in scala 2.12.4:
https://docs.scala-lang.org/overviews/macros/bundles.html

  1. the following compiles fine:
import scala.language.experimental.macros
import scala.reflect.macros.whitebox.Context
object Macros {
  def mono : Unit = macro MacrosImpl.mono
  def poly[ T ] : String = macro MacrosImpl.poly[ T ]
}
class MacrosImpl( val c : Context ) {
  import c.universe._
  def mono : c.Tree = q"${println( 1 )}"
  def poly[ T : c.WeakTypeTag ] : c.Expr[ String ] = c.Expr[ String ]( q"${}" )
}
  1. but after renaming val c : Context -> val ccc : Context compilation fails:
import scala.language.experimental.macros
import scala.reflect.macros.whitebox.Context
object Macros {
// error: bundle implementation has incompatible shape: required: : MacrosImpl.this.<none>.Expr[Unit] or   
  def mono : Unit = macro MacrosImpl.mono
// error: bundle implementations cannot have implicit parameters other than WeakTypeTag evidences
  def poly[ T ] : String = macro MacrosImpl.poly[ T ]
}
class MacrosImpl( val ccc : Context ) {
  import ccc.universe._
  def mono : ccc.Tree = q"${println( 1 )}"
  def poly[ T : ccc.WeakTypeTag ] : ccc.Expr[ String ] = ccc.Expr[ String ]( q"${}" )
}

so, c must stand for something really important? :-)

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions