Skip to content

initializer is inaccessible due to 'private' protection level error when not all struct properties are private #64738

Open
@simanerush

Description

@simanerush

Description

Consider the following code:

struct Struct {
  var x: Int

  private var y: Int = 0
}
do {
  let _ = Struct(x: 0)
}

Despite x not being private, the "inaccessible initializer" error is emitted.

One fix is to change y to a constant so that it doesn’t contribute to the generated initializer:

struct Struct {
  var x: Int

  private let y: Int = 0
}

However, this is not always a desired behavior.

Solution

Generate an additional, internal memberwise initializer for x, or more fine control over which properties contribute to the generated init.

Metadata

Metadata

Assignees

No one assigned

    Labels

    access controlFeature → modifiers: Access control and access levelscompilerThe Swift compiler itselffeatureA feature request or implementationmemberwise initFeature: Memberwise structure initializersswift evolution proposal neededFlag → feature: A feature that warrants a Swift evolution proposal

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions