swift - how can i use `#function` symbol in a `inline` function? -


i'd use name of function resolve problems, #function seems not work @inline(__always), here codes:

@inline(__always) func log() {   print(#function) } func a() { log() } // want 'a()', got 'log()' func b() { log() } func c() { log() } //... 

can explain? or that's stupid idea.

if intention print name of function calls log(), should pass default argument (which evaluated in context of caller), demonstrated in building assert() in swift, part 2: __file__ , __line__ in swift blog.

example:

@inline(__always) func log(_ message: string, callingfunction: string = #function) {     print("\(callingfunction): \(message)") }  func a() { log("hello world") } func b() { log("foo") } func c() { log("bar") }  a() // a(): hello world b() // b(): foo c() // c(): bar 

this works regardless of whether log function inlined or not. (inlining not change semantics of program. in particular, not mean source code of func log included source code of func a() , compiled single function.)


Comments

Popular posts from this blog

Command prompt result in label. Python 2.7 -

javascript - How do I use URL parameters to change link href on page? -

amazon web services - AWS Route53 Trying To Get Site To Resolve To www -