scala的宏定义

本文介绍了Scala的宏定义,包括从简单的“Hello World!”宏到实现一个用于调试的宏。通过逐步示例,解释了如何使用宏来打印参数,并展示了如何构建更复杂的单变量和多变量调试宏。文章还提供了实验项目的设置指导和代码示例。
摘要由CSDN通过智能技术生成

其实宏的使用并不难,api已经帮我们做好了一切,我们只要关注如何使用获取宏参数和宏的返回值

一个例子:

import scala.reflect.macros.Context
import scala.collection.mutable.{ListBuffer, Stack}

object QStringImpl {

 def Qmap(p: Char => Int,elem:String):Int = macro QStringImpl.map_Impl
 def map_Impl(c: Context)(p: c.Expr[Char => Int],elem: c.Expr[String]): c.Expr[Int] = {
        	import c.universe._
 			//splice只能在reify使用,具体化,功能与reify相反
        	reify { elem.splice.map(p.splice).sum}
        }
}


 

import scala.language.experimental.macros

object DemoTest2 extends App{
	import QStringImpl._
	val result = Qmap(_.toInt + 1,"111111")
	println(result)
}


在scala中也有官方的实例,不过都是别人写的,个人觉得写得不好!正如我在上面说的,无论scala的宏是如何被官方实现的,对于程序员只需知道如何获取参数和返回参数,这是官方doc的教程

http://docs.scala-lang.org/overviews/macros/overview.html

自已google一下,找到一个比较好例子,很容易的可以理解了,对于宏的使用要注意全局变量的依赖

链接http://www.warski.org/blog/2012/12/starting-with-scala-macros-a-short-tutorial/

顺便贴出来

Starting with Scala Macros: a short tutorial

Using some time during the weekend, I decided to finally explore one the new features in the coming Scala 2.10, macros. Macros are also written in Scala so in essence a macro is a piece of Scala code, executed at compile-time, which manipulates and modifies the AST of a Scala program.

To do something useful, I wanted to implement a simple macro for debugging; I suppose I’m not alone in using println-debugging, that is debugging by inserting statements like:

1
println("After register; user = " + user + ", userCount = " + userCount)

running a test, and checking what the output is. Writing the variable name before the variable is tedious, so I wanted to write a macro which would do that for me; that is:

1
debug("After register", user, userCount)

should have the same effect as the first snippet (it should generate code similar to the one above).

Let’s see step-by-step how to implement such a macro. There’s a good getting started guide on the scala macros page, which I used. All code explained below is available on GitHub, in the scala-macro-debug project.

1. PROJECT SETUP

To experiment comfortably we’ll need to setup a simple project first. We will need at least two subprojects: one for macros, and one for testing the macros. That is because the macros must be compiled separately and before and code that uses them (as they influence the compilation process).

Moreover, the macro subproject needs to have a dependency on scala-compiler, to be able to access the reflection and AST classes.

A simple SBT build file could look like this: Build.scala.

2. HELLO WORLD!

“Hello World!” is always a great starting point. So my first step was to write a macro, which would expand hello() to println("Hello World!") at compile-time.

In the macros subproject, we have to create a new object, which defines hello() and the macro:

1
2
3
4
5
6
7
8
9
10
11
12
13
package com.softwaremill.debug
 
import language.experimental.macros
 
import reflect.macros.Context
 
object DebugMacros 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值