gohanlon / swift-memberwise-init-macro

Swift Macro for enhanced automatic inits.
MIT License
122 stars 8 forks source link

Using @Model with @MemberwiseInit in a class doesn't seem to generate the init #33

Closed rursache closed 7 months ago

rursache commented 7 months ago

Description

import Foundation
import SwiftData
import MemberwiseInit

@Model
@MemberwiseInit class Test {
    var works: String?
}

Checklist

Expected behavior

Test(works: "Yes!") should work

Actual behavior

Test(works: "Yes!") outputs error @Model requires an initializer be provided for 'Test'

swift-memberwise-init-macro version information

0.3.0 / main branch

Destination operating system

macOS 14.4.1 (23E224)

Xcode version information

Xcode 15.3 (15E204a)

Swift Compiler version information

swift-driver version: 1.90.11.1 Apple Swift version 5.10 (swiftlang-5.10.0.13 clang-1500.3.9.4)
Target: x86_64-apple-macosx14.0
gohanlon commented 7 months ago

Unfortunately, I don't think this is possible to fix within MemberwiseInit. Further, it may not even be addressable within the design of Swift Macros: by design, @Model can't "see" the members added by @MemberwiseInit.

As it stands, adding a manual initializer seems the only viable workaround. You might try raising the issue with Apple, which would need to be sent using Feedback Assistant.

For completeness, the @Model macro's implementation is inspecting the syntax of the class, and emitting an error when that syntax doesn't include an initializer:

@Model
┬─────
╰─ 🛑 @Model requires an initializer be provided for 'Test'
@MemberwiseInit class Test {  
  var works: String?
}

(Reordering the macros here or in general has no effect.)

rursache commented 7 months ago

@gohanlon thanks for the long and detailed answer, I really appreciate it! i'm nee to macros and SwiftData, hoped to get rid of the lengthy boilerplate inits