Groovy电源打印 [英] Groovy Power Print

查看:121
本文介绍了Groovy电源打印的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Groovy有一个权力断言,但我想要一个权力的打印。例如,

  def foo ='banna5',monkey = 7,x = 70 
printlnfoo = $ foo,monkey = $ monkey,x / 2 = $ {x / 2}

foo,monkey和println行中的表达式......我想输入的是这个,

  pprint foo,monkey, x / 2 

这是我期望的输出(来自println或pprintln),

  foo = banna5,monkey = 7,x / 2 = 35 

有没有这样做的小窍门?

解决方案

在Groovy中你现在想做什么......



问题在于当你打印出值时获取变量的名字。



Powerrassert似乎这样做的方式是通过对字节码的AST操作(参见 class 947 of the class org.codehaus.groovy.classgen.AsmClassGenerator )来装饰在 org.codehaus.groovy.runtime.powerassert 包中包含几个类的assert方法。



您需要的结果,类似的东西将不得不被添加来以类似的方式操纵AST。



您可以将一个新的功能问题添加到项目的JIRA ,你永远不会知道......它可能会使它成为未来的Groovy版本。



但是现在,我认为你已经被你的println卡住了,因为你已经拥有它了。

Groovy has a power assert, but I'd like a power print. For example,

def foo = 'banna5', monkey=7, x=70
println "foo=$foo, monkey=$monkey, x/2=${x/2}"

See the repeating foo, monkey, and expression in the println line... what I want to type is this,

pprint foo, monkey, x/2

This is the output I expect (from either println or pprintln),

foo=banna5, monkey=7, x/2=35

Is there a ditty that does this already?

解决方案

There is nothing that does what you want currently in Groovy...

The problem would be getting the names of the variables when you print out the values.

The way that powerrassert seems to do it, is via AST manipulation of the bytecode (see line 947 of the class org.codehaus.groovy.classgen.AsmClassGenerator) to decorate the assert method with several classes inside the org.codehaus.groovy.runtime.powerassert package.

So to get the results you require, something similar would have to be added to manipulate the AST in a similar way.

You could add a new feature issue to the project's JIRA, and you never know...it might make it into future versions of Groovy

But for now, I think you're stuck with your println as you have it above

这篇关于Groovy电源打印的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆