Skip to content

@Observable Macro causes compilation error when in an extension of a view #66916

@ngimelliUW

Description

@ngimelliUW

Description

The @Observable macro only works on view models that are not within extensions. For example this works:

struct MyView: View {
  ...
}

extension MyView {
  final class MyViewModel: ObservableObject {
    ...
  }
} 

but this does not:

extension MyView {
  @Observable final class MyViewModel {
    ...
  }
} 

Steps to reproduce

  1. Create a simple view with a basic view model using the @Observable macro.
  2. Wrap the view model in an extension of the view; this should cause the error.

Expected behavior
Successful compilation.

Environment

  • Swift compiler version info: swift-driver version: 1.75.2 Apple Swift version 5.8.1 (swiftlang-5.8.0.124.5 clang-1403.0.22.11.100)
    Target: arm64-apple-macosx13.0
  • Xcode 15.0 Beta
  • Deployment target: iOS 17.0 Beta

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugA deviation from expected or documented behavior. Also: expected but undesirable behavior.triage neededThis issue needs more specific labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions