Skip to content

Commit

Permalink
Release post-16
Browse files Browse the repository at this point in the history
  • Loading branch information
DandyLyons committed Sep 24, 2024
1 parent 8ab3073 commit bd5f6ed
Show file tree
Hide file tree
Showing 4 changed files with 119 additions and 9 deletions.
2 changes: 1 addition & 1 deletion content/en/posts/post-15/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ date: 2024-09-23
title: Demystifying Modern macOS Development
slug: demystifying-modern-macos-development
images: [""]
description:
description: Learn the subtle differences between native Mac apps, Mac Catalyst apps, and Mac (Designed for iPad) apps.
topics: [macOS, SwiftUI, Catalyst, Swift]
---

Expand Down
68 changes: 68 additions & 0 deletions content/en/posts/post-16/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
---
date: 2024-09-24
title: Swift 6's New @retroactive Attribute
slug: swift-6-retroactive-attribute
images: [""]
description: Learn about retroactive protocol conformance, and why you probably shouldn't use it on external types.
topics: [Swift]
---

# Swift 6's New `@retroactive` Attribute

Swift 6.0 introduced the `@retroactive` attribute to address a specific issue with protocol conformances. Here's what you need to know:

## The Problem

Suppose you are using a type from an external library and realize that the type does not conform to a protocol such as `Codable`. You might be tempted to add your own conformance.

```swift
import ExternalLibrary
extension ExternalType: Codable {
// implementation here
}
```

However, doing this can be quite problematic. What happens if the library owner later adds their own conformance? Which code will execute? Your conformance or their conformance? The answer is that the behavior will be undefined at runtime, since we don't know which conformance will "win". Even worse, this same problem will propagate to every library that imports your library.[^1]

[^1]: There are a few exceptions to the this which you can find [here](https://github.com/swiftlang/swift-evolution/blob/main/proposals/0364-retroactive-conformance-warning.md#detailed-design) and [here](https://forums.swift.org/t/amendment-se-0364-allow-same-package-conformances/71877).

## The Solution: `@retroactive`

To combat this problem, Swift 6.0 now emits a warning any time you retroactively add a conformance to an external type. However, there are some scenarios where it might be best to extend external types, despite this risk.

So, the `@retroactive` attribute allows you to explicitly declare that you are intentionally adding a conformance that might conflict with future updates to the original module.

## When to Use It
**⚠️ You probably shouldn't use `@retroactive`.**
Consider it a code smell.

If you **must** use it, then be sure to check every time you update your dependency to a new version. If they have added the conformance themselves, then this will create a conflict.

If you use `@retroactive`, you are, in fact, explicitly declaring that you acknowledge the risk and are willing to take responsibility for potential future conflicts.

## How to Use It

```swift
extension ExternalType: @retroactive ExternalProtocol {
// Implementation here
}
```


## Alternative (for pre-Swift 6)

If you need to support older Swift language modes, you can silence the warning by fully qualifying the types:

```swift
extension Module.ExternalType: OtherModule.ExternalProtocol {
// Implementation here
}
```

## Conclusion
Remember, while `@retroactive` provides a solution, it's best to avoid adding conformances to external types and protocols whenever possible to maintain better compatibility and reduce potential conflicts.


## Recommended Reading
- Read the full Swift Evolution proposal for more [info](https://github.com/swiftlang/swift-evolution/blob/main/proposals/0364-retroactive-conformance-warning.md).
- [Extensions in Swift: How and when to use them - SwiftLee](https://www.avanderlee.com/swift/extensions/)
50 changes: 50 additions & 0 deletions content/en/unlisted/Cursor IDE with Swift/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
---
draft: true
date: 2024-09-17
title: Exploring AI Powered Swift Development in Cursor IDE
slug: exploring-ai-powered-swift-dev-in-cursor
description: Is Cursor IDE's Copilot a good fit for Swift development?
topics: ["Cursor IDE", "Swift"]
---

# Exploring AI Powered Swift Development in Cursor IDE


## Myths About Swift Development
- You can only develop Swift in Xcode
- Swift in VS Code is bad/doesn't work

### Actual Limitations
- Only Xcode has the iOS SDK.

## Why Use AI to Develop
- Ask questions about your code
- Write code faster

## Reasons to consider using Cursor
- Run AI off your device (unlike Xcode 16)
- The LLM has project wide context:
- `.cursorrules` file allows you to give project specific prompting
- Type `@` symbol to add documentation to your prompts
- Cursor Composer: Multi-file editing.

## About Cursor
- Cursor is a fork of VS Code.
- Cursor makes it ridiculously easy to switch from VS Code:
- It ports all your settings, keyboard shortcuts, and plugins.

## Setup
The setup for Cursor is basically identical to VS Code but with a few recommended extra steps.

### Install the Swift Extension
Install the Swift Extension if you haven't done so already.

### Index the Documentation in Cursor


### Add .cursorrules
Each project in Cursor can optionally have a `.cursorrules` file. This is a file where you can put plain English instructions[^1]

[^1]: You can of course use other languages to provide prompts to the LLMs. In fact, that could be very helpful for your particular use case. However, the vast majority of the relevant text that these LLMs have been trained on is in English. In other words, you are likely to get better results if your prompts are in English. But fret not, even if English isn't your main language, LLMs are also very good at translation. Try prompts in your language first, and if the performance is sub-par, try translating your prompts into English to see if you get better results.

## Conclusion: What is The Future of Software Engineering?
8 changes: 0 additions & 8 deletions content/en/unlisted/testing in TCA/index.md

This file was deleted.

0 comments on commit bd5f6ed

Please sign in to comment.