Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
simanerush opened this issue Mar 29, 2023 · 3 comments
Labels
access control Feature → modifiers: Access control and access levels compiler The Swift compiler in itself feature A feature request or implementation memberwise init Feature: Memberwise structure initializers swift evolution proposal needed Flag → feature: A feature that warrants a Swift evolution proposal

Comments

@simanerush
Copy link
Member

simanerush commented Mar 29, 2023

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.

@simanerush simanerush added bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. triage needed This issue needs more specific labels labels Mar 29, 2023
@simanerush simanerush changed the title initializer is inaccessible due to 'private' protection level error for stunts with private variables with default values. initializer is inaccessible due to 'private' protection level error for structs with private variables with default values. Mar 29, 2023
@aabagdi
Copy link

aabagdi commented Mar 29, 2023

A fix would be much appreciated!

@AnthonyLatsis AnthonyLatsis added compiler The Swift compiler in itself feature A feature request or implementation access control Feature → modifiers: Access control and access levels memberwise init Feature: Memberwise structure initializers and removed bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. triage needed This issue needs more specific labels labels Mar 30, 2023
@AnthonyLatsis AnthonyLatsis changed the title initializer is inaccessible due to 'private' protection level error for structs with private variables with default values. initializer is inaccessible due to 'private' protection level error when not all struct properties are private Mar 30, 2023
@AnthonyLatsis AnthonyLatsis added the swift evolution proposal needed Flag → feature: A feature that warrants a Swift evolution proposal label Mar 30, 2023
@hassila
Copy link

hassila commented Mar 30, 2023

You might want to check out the discussion and references here:

https://forums.swift.org/t/pitch-synthesize-init-of-structs/63688

@mikkpav
Copy link

mikkpav commented Jan 18, 2024

Using this also works:
private(set) var y: Int = 0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
access control Feature → modifiers: Access control and access levels compiler The Swift compiler in itself feature A feature request or implementation memberwise init Feature: Memberwise structure initializers swift evolution proposal needed Flag → feature: A feature that warrants a Swift evolution proposal
Projects
None yet
Development

No branches or pull requests

5 participants