# Compiler Directives

{% tabs %}
{% tab title="📗 參考" %}
* [ ] [Using compiler directives in Swift](https://www.swiftbysundell.com/articles/using-compiler-directives-in-swift/) - Sundell
* [ ] [Feature flags in Swift](https://www.swiftbysundell.com/articles/feature-flags-in-swift/) - Sundell
* [ ] [Compiler Directives](https://docs.elementscompiler.com/Compiler/Directives/) - Elements Docs
* [x] [Understanding @inlinable in Swift](https://swiftrocks.com/understanding-inlinable-in-swift) - SwiftRocks
{% endtab %}

{% tab title="👥 相關" %}
* Swift ⟩ [attributes](../../swift/attributes "mention")
* [debugging](../../swift/debugging "mention")
{% endtab %}

{% tab title="🗣 討論" %}
* [Macros in Swift?](https://stackoverflow.com/a/24116621/5409815) - custom log function using [#file, #line, #function](directives/file-line-function) ⭐️
{% endtab %}
{% endtabs %}

## Compilation Condition: #if

* \#if **DEBUG**
* \#if \<compilation flag>  👉  [Feature flags in Swift](https://www.swiftbysundell.com/articles/feature-flags-in-swift/)
* \#if **targetEnvironment**( )

{% tabs %}
{% tab title="#if DEBUG" %}
```swift
func log(_ expression: @autoclosure () -> Any) {
    // print a given expression only within debug builds
    #if DEBUG
    print(expression())
    #endif
}
```
{% endtab %}

{% tab title="#if os( )" %}
```swift
struct EditorView: View {
    //...
    var body: some View {
    
        #if os(tvOS)
        CanvasView()
        #else
        CanvasView().gesture(DragGesture().onChanged { state in
          // ...
        })
        #endif
        // ...
    }
}
```
{% endtab %}

{% tab title="#if targetEnvironment( )" %}
```swift
func setupTabBarController(_ controller: UITabBarController) {
    var viewControllers = [UIViewController]()

    #if targetEnvironment(simulator)
    viewControllers.append(DebugViewController())
    #endif

    controller.viewControllers = viewControllers
}
```
{% endtab %}
{% endtabs %}
