Why isn't a private var in Swift struct available in #if'd init for that struct?

已回答
While doing some research on ncurses and how to use it in Swift, I took a look at IOGUI[1].
 
It wouldn't compile properly, so I began clearing warnings and errors.
 
That's when I came across what may be some Swift subtleness that I'm unaware of.
 
> Note: I'm not discussing compilation errors below. This question is specific to the parsing done by Xcode to indicate warnings and errors in the code editor.
 
Referring to the following struct:
 
```swift
public struct MenuWidget {
 
    var widgetRows: Int
    // CUT
    private var startRow: Int
 
#if os(Linux)
    public init(startRow: Int, widgetSize: Int, choices: [GUIMenuChoices], delegate: @escaping MenuChoicesSelectionDelegate, mainWindow: UnsafeMutablePointer<WINDOW>) {
 
        self.startRow = startRow
        // CUT
        initWindows()
    }
#else
    public init(startRow: Int, widgetSize: Int, choices: [GUIMenuChoices], delegate: @escaping MenuChoicesSelectionDelegate, mainWindow: OpaquePointer) {
 
        self.startRow = startRow
        // CUT
        initWindows()
    }
#endif
 
    // CUT
} // END STRUCT
```
 
Within the `#if os(Linux)` section, `self.startRow` is marked as inaccessible from that scope.
 
Yet in the `#else`, there is no error indicated. `widgetRows` is accessible in both the `#if` and `#else`
 
If I remove private from the `startRow` declaration, then it's fine in both scopes. But that makes the variable `internal` and makes it accessible outside of the struct, which is incorrect.
 
It could also just be the behavior of the Swift `#if` construct that I'm not familiar with.
 
I've searched the usual places, including here at SO. Nothing similar deals with this particular scenario, at least not with the search terms I used.
 
Any hints or tips would be appreciated.
 
P.S. If you want to check out the IOGUI[1] source, I'm specifically referring to lines 41 and 78-100 (inclusive) of MenuWidget.swift. A second example are lines 72-108 (inclusive) of InputPopupWidget.swift.
 
 
 
0
Avatar
Permanently deleted user
正式评论

Hi David,

Created OC-18354 per you report. Please watch the ticket.

请先登录再写评论。