Skip to content

Prepare README for 0.5.0 release #42

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

Merged
merged 1 commit into from
Jul 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 42 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ Informed by explicit developer cues, MemberwiseInit can more often automatically
* [Attributed properties are ignored by default, but includable](#attributed-properties-are-ignored-by-default-but-includable)
* [Support for property wrappers](#support-for-property-wrappers)
* [Automatic `@escaping` for closure types (usually)](#automatic-escaping-for-closure-types-usually)
* [Experimental: Unchecked memberwise initialization](#experimental-unchecked-memberwise-initialization)
* [Experimental: Deunderscore parameter names](#experimental-deunderscore-parameter-names)
* [Experimental: Defaulting optionals to nil](#experimental-defaulting-optionals-to-nil)
* [Tuple destructuring in property declarations isn’t supported (yet)](#tuple-destructuring-in-property-declarations-isnt-supported-yet)
Expand All @@ -45,7 +46,7 @@ To use MemberwiseInit:

```swift
dependencies: [
.package(url: "https://github.com/gohanlon/swift-memberwise-init-macro", from: "0.4.0")
.package(url: "https://github.com/gohanlon/swift-memberwise-init-macro", from: "0.5.0")
]
```

Expand Down Expand Up @@ -176,6 +177,9 @@ Attach to the property declarations of a struct that `@MemberwiseInit` is provid
* `@MemberwiseInit` on `actor`, `class` *(experimental)*
<br> Attachable to actor and class.

* `@_UncheckedMemberwiseInit` *(experimental)*
<br> Generate a memberwise initializer for all properties, regardless of access level, with reduced compile-time safety checks (compared to `@MemberwiseInit`).

## Features and limitations

### Custom `init` parameter labels
Expand Down Expand Up @@ -598,6 +602,43 @@ public init(
}
```

### Experimental: Unchecked memberwise initialization

`@_UncheckedMemberwiseInit` is an experimental macro that bypasses compile-time safety checks and strict access control enforcement. It generates an initializer for all properties of a type, regardless of their declared access levels. Use it judiciously.

Key characteristics:

- Generates an initializer that includes all properties, regardless of their declared access levels
- Includes attributed properties by default (differs from `@MemberwiseInit`)
- Follows the same usage pattern as `@MemberwiseInit`

Example:

```swift
@_UncheckedMemberwiseInit(.public)
public struct APIResponse: Codable {
public let id: String
@Monitored internal var statusCode: Int
private var rawResponse: Data

// Computed properties and methods...
}
```

This yields a public initializer that includes all properties, regardless of their access level or attributes. Unlike `@MemberwiseInit`, this macro doesn't require `@Init` annotations or any other explicit opt-ins. The resulting initializer is:

```swift
public init(
id: String,
statusCode: Int,
rawResponse: Data
) {
self.id = id
self.statusCode = statusCode
self.rawResponse = rawResponse
}
```

### Experimental: Deunderscore parameter names

> **Note**
Expand Down
36 changes: 36 additions & 0 deletions Tests/MemberwiseInitTests/ReadmeTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ final class ReadmeTests: XCTestCase {
macros: [
"MemberwiseInit": MemberwiseInitMacro.self,
"Init": InitMacro.self,
"_UncheckedMemberwiseInit": UncheckedMemberwiseInitMacro.self,
]
) {
super.invokeTest()
Expand Down Expand Up @@ -526,6 +527,41 @@ final class ReadmeTests: XCTestCase {
}
}

func testUncheckedMemberwiseInit() {
assertMacro {
"""
@_UncheckedMemberwiseInit(.internal)
public struct APIResponse: Codable {
public let id: String
@Monitored internal var statusCode: Int
private var rawResponse: Data

// Computed properties and methods...
}
"""
} expansion: {
"""
public struct APIResponse: Codable {
public let id: String
@Monitored internal var statusCode: Int
private var rawResponse: Data

internal init(
id: String,
statusCode: Int,
rawResponse: Data
) {
self.id = id
self.statusCode = statusCode
self.rawResponse = rawResponse
}

// Computed properties and methods...
}
"""
}
}

func testDeunderscoreParameterNames() {
assertMacro {
"""
Expand Down