Advertisement
Guest User

Macro

a guest
Feb 25th, 2014
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scala 0.90 KB | None | 0 0
  1. import java.io.File
  2. import language.experimental.macros
  3. import scala.reflect.macros.Context
  4.  
  5.  
  6. object Macros {
  7.  
  8.   def L_ : Int = macro lineImpl
  9.  
  10.   def lineImpl( c: Context ): c.Expr[Int] = {
  11.     import c.universe._
  12.  
  13.     val line = Literal( Constant( c.enclosingPosition.line ) )
  14.  
  15.     c.Expr[Int]( line )
  16.   }
  17.  
  18.   def F_ : String = macro fileImpl
  19.  
  20.   def fileImpl( c: Context ): c.Expr[String] = {
  21.     import c.universe._
  22.  
  23.     val absolute = c.enclosingPosition.source.file.file.toURI
  24.     val base = new File( "." ).toURI
  25.  
  26.     val path = Literal( Constant( c.enclosingPosition.source.file.file.getName() ) )
  27.  
  28.     c.Expr[String]( path )
  29.   }
  30.  
  31.   def C_ : String = macro classImpl
  32.  
  33.   def classImpl( c: Context ): c.Expr[String] = {
  34.     import c.universe._
  35.    
  36.     val class_ = Literal( Constant( c.enclosingClass.toString.split(" ")( 1 ) ) )  
  37.     c.Expr[String]( class_ )
  38.   }
  39.  
  40. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement