# Conditional Compilation inside array initializer

**URL:** <https://forums.swift.org/t/conditional-compilation-inside-array-initializer/53792>\
**Category:** Using Swift\
**Created:** [November 30, 2021, 8:03pm UTC](https://forums.swift.org/t/conditional-compilation-inside-array-initializer/53792 "2021-11-30T20:03:18Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Hacksaw](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/hacksaw/32/32775_2.png) [@Hacksaw](https://forums.swift.org/u/Hacksaw)\
**Post date:** [November 30, 2021, 8:03pm UTC](https://forums.swift.org/t/conditional-compilation-inside-array-initializer/53792/1 "2021-11-30T20:03:18Z")

</div>

I want something like this:

```swift
import SwiftUI

let screens: [AnyView] = [
    #if os(iOS)
    AnyView(TextFieldPlay()),
    #endif
    AnyView(PickerPlay()),
    AnyView(ViewSizingPlay()),
    AnyView(ProgressBarPlay()),
    AnyView(HalfRoundedPlay()),
    AnyView(MultiBarPlay()),
    AnyView(ClockFacePlay()),
    AnyView(BlurPlay()),
    AnyView(DnDPlay()),
    AnyView(ColorPatchPlay()),
    AnyView(CanvasPlay()),
    AnyView(SKPlay())
]

```

But no go, Xcode complains that "Consecutive statements on a line must be separated by ';' ", so clearly it's getting terminally confused.

I get a similar message if I try `if #available`, which really is no surprise.

Is there any pretty way to do this?

Also, if anyone wanted to offer suggestions about the array itself, it'd be welcome. The program uses the array to populate a navigation grid. I really wish it could be an array of View but that never worked out.

---

<div class="post-metadata">

**Author:** ![ahti](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/ahti/32/604_2.png) [@ahti](https://forums.swift.org/u/ahti)\
**Post date:** [November 30, 2021, 8:18pm UTC](https://forums.swift.org/t/conditional-compilation-inside-array-initializer/53792/2 "2021-11-30T20:18:31Z")

</div>

> [@Hacksaw](#):
>
> Is there any pretty way to do this?

Right now, not really. I think I'd go for something like this, which is quite a bit more verbose:

```swift
let lol: [String] = {
    var a = [
        "a",
    ]
    #if SOME_CONDITION
    a += ["x"]
    #endif
    a += ["y"]
    return a
}()

```

In the long run, the next revision of [SE-0330](https://forums.swift.org/t/se-0330-conditionals-in-collections/53375/22) is our best hope.

---

<div class="post-metadata">

**Author:** ![ahti](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/ahti/32/604_2.png) [@ahti](https://forums.swift.org/u/ahti)\
**Post date:** [November 30, 2021, 9:02pm UTC](https://forums.swift.org/t/conditional-compilation-inside-array-initializer/53792/3 "2021-11-30T21:02:58Z")

</div>

Or, if you are okay with using something that might look a bit confusing to newcomers to your code base, you can use a result builder, something like this:

```swift
@resultBuilder struct ArrayBuilder<T> {
    static func buildBlock(_ components: T...) -> [T] { components }
}

extension Array {
    init(@ArrayBuilder<Element> builder: () -> [Element]) { self.init(builder()) }
}

Array {
    "a"
    #if KERN_KDEBUG
    "c"
    #endif
    "b"
}

```

---

<div class="post-metadata">

**Author:** ![Hacksaw](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/hacksaw/32/32775_2.png) [@Hacksaw](https://forums.swift.org/u/Hacksaw)\
**Post date:** [November 30, 2021, 9:04pm UTC](https://forums.swift.org/t/conditional-compilation-inside-array-initializer/53792/4 "2021-11-30T21:04:54Z")

</div>

That's tempting just for fun, but in the end I went with this:

```swift
let screens: [AnyView] = {
    var theSet: [AnyView] = []
    #if os(iOS)
    theSet.append(AnyView(TextFieldPlay()))
    #endif
    
    theSet.append (contentsOf: [
        AnyView(PickerPlay()),
        AnyView(ViewSizingPlay()),
        AnyView(ProgressBarPlay()),
        AnyView(HalfRoundedPlay()),
        AnyView(MultiBarPlay()),
        AnyView(ClockFacePlay()),
        AnyView(BlurPlay()),
        AnyView(DnDPlay()),
        AnyView(ColorPatchPlay()),
        AnyView(CanvasPlay()),
        AnyView(SKPlay())
    ])
    return theSet
}()

```
