将函数的左大括号移到下一行是一种常见做法。如何使用 astyle(代码美化器)在类方法中应用它?
例子:
// this is an initial C++ code
class Class
{
public:
static int foo(bool x) {
if (x) {
return 42;
} else {
return 0;
}
}
};
Run Code Online (Sandbox Code Playgroud)
修改后的版本应该是:
class Class
{
public:
static int foo(bool x)
{ // this brace in next line
if (x) {
return 42;
} else {
return 0;
}
}
};
Run Code Online (Sandbox Code Playgroud)
我所有的尝试都只适用于全局函数。
我想知道是否有任何惯用的方式来表示作用域语义.通过范围我的意思是:
前两个项目符号的示例代码:
package main
import "log"
import "sync"
func Scoped(m *sync.Mutex) func() {
m.Lock()
return func() {
m.Unlock()
}
}
func Log(what string) func() {
log.Println(what, "started")
return func() {
log.Println(what, "done")
}
}
func main() {
defer Log("testing")()
m := &sync.Mutex{} // obviously mutex should be from other source in real life
defer Scoped(m)()
// use m
}
Run Code Online (Sandbox Code Playgroud)
https://play.golang.org/p/33j-GrBWSq
基本上,我们需要做一个函数调用刚才(如互斥锁),和一个呼叫应该推迟推迟(如互斥解锁).我建议只返回未命名的函数,但它可以很容易地命名(返回带有函数字段的struct).
只有一个问题:用户可能忘记"呼叫"第一次呼叫的结果.
这段代码是(可以)惯用的吗?