-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
8ab3073
commit bd5f6ed
Showing
4 changed files
with
119 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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/) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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? |
This file was deleted.
Oops, something went wrong.