#file, #line, #function
temp
💾 程式: replit
// ----------------------
// ⭐ log function
// ----------------------
func log(
_ message: String,
// file : String = #file,
line : Int = #line,
function : String = #function,
note : String? = nil // additional info
) {
print("ℹ️ \(message)\n (func: '\(function)', line: \(line))\n " + (note ?? ""))
}
// ----------------------------------------
// ⭐ top level: current module name
// ----------------------------------------
log("top level")
// ---------------------------------------------
// ⭐ inside getter/setter: property name
// ---------------------------------------------
var freeVar: Int {
get { log("inside getter"); return 0 } // freeVar ⭐
set { log("inside setter") } // freeVar ⭐
}
// call getter/setter
let x = freeVar
freeVar = 1
// ------------------------------------------------------
// ⭐ inside function: function name (with params)
// ------------------------------------------------------
func f(x: Int) {
log("inside function") // f(x:) ⭐
}
func add(_ a: Int, _ b: Int) {
log("inside function", note: "(column: \(#column))\n") // add(_:_:) ⭐
// (line: 44, column: 47)
}
// call the functions
f(x: 1)
add(1, 2)
// ---------------------------------------------
// ⭐ inside init/subscript: that keyword
// ⭐ inside method : method name
// ---------------------------------------------
struct A {
init(value: Int) {
log("inside 'init'") // init(value:) ⭐
}
subscript(index: Int) -> Int {
log("inside 'subscript'") // subscript(_:) ⭐
return 0
}
func method(x: Int, y: Int) {
log("inside method") // method(x:y:) ⭐
}
}
// call these members
let a = A(value: 1)
_ = a[0]
a.method(x: 1, y: 2)
Last updated
Was this helpful?