Does Swift currently support macros, or are there future plans to add support? Currently I'm scattering:
Log.trace(nil, function: __FUNCTION__, file: __FILE__, line: __LINE__)
In various places throughout my code.
Where you typically used the #define directive to define a primitive constant in C and Objective-C, in Swift you use a global constant instead. For example, the constant definition #define FADE_ANIMATION_DURATION 0.35 can be better expressed in Swift with let FADE_ANIMATION_DURATION = 0.35 .
Writing a macro is another way of ensuring modular programming in assembly language. A macro is a sequence of instructions, assigned by a name and could be used anywhere in the program. In NASM, macros are defined with %macro and %endmacro directives.
Macros can be used in many languages, it's not a specialty of objective-c language. Macros are preprocessor definitions. What this means is that before your code is compiled, the preprocessor scans your code and, amongst other things, substitutes the definition of your macro wherever it sees the name of your macro.
A macro is an automated input sequence that imitates keystrokes or mouse actions. A macro is typically used to replace a repetitive series of keyboard and mouse actions and used often in spreadsheets and word processing applications like MS Excel and MS Word.
In this case you should add a default value for the "macro" parameters.
Swift 2.2 and higher
func log(message: String, function: String = #function, file: String = #file, line: Int = #line) { print("Message \"\(message)\" (File: \(file), Function: \(function), Line: \(line))") } log("Some message")
Swift 2.1 and lower
func log(message: String, function: String = __FUNCTION__, file: String = __FILE__, line: Int = __LINE__) { print("Message \"\(message)\" (File: \(file.lastPathComponent), Function: \(function), Line: \(line))") } log("Some message")
This is what fatalError
and assert
functions do.
There are no other macros except the conditional compilation already mentioned in another answer.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With