74 函数拦截
FunctionInterceptor 包裹声明侧的 greet 调用。around 调用一次 proceed(),因此原函数仍产生问候文本。
norm
import std.annotation.FunctionInterceptor
import std.annotation.SourceRetention
annotation Trace implements FunctionInterceptor, SourceRetention {
R around<R>(FunctionInvocation<R> invocation) {
printLine("enter")
R result = invocation.proceed()
printLine("leave")
return result
}
}
@Trace()
String greet(String name) { return "Hello, " + name }
main() { printLine(greet(name: "Norm")) }预期输出:
text
enter
leave
Hello, Norm动手试试:删除 proceed(),观察必须如何提供返回值。
精确规则:参考。