Re: Swift in the browser, @State as a macro & AI function bodies
Hi everyone,
In this issue, you’re in for a real treat - I’ve scoured my database of collected links over the past few weeks and selected some truly mind-blowing articles and tools.
We’ll look at Swift macros - specifically how the new @State macro works in Xcode 27, and how Miguel de Icaza’s AgenticSwift lets you write function bodies in plain English using an @ai macro.
Speaking of Swift - how about running Swift in the browser? Or - even wilder - writing and running Swift in an entirely browser-based IDE? Check out MiniSwift (and try the interactive playground demo right in the issue!).
We’ll also look at hooks for Apple’s Foundation Models framework, self-sizing sheets in SwiftUI, and travel back 60 years in computing history to ELIZA!
Let me know your thoughts by hitting reply or tagging me on your social platform of choice!
You can use Swift on your phone, tablet, computer, on the command line, in the cloud, and even on embedded devices.
But did you ever consider running Swift code in the browser? With MiniSwift, now you can.
Ugur Toprakdeviren not only built a toolchain that translates Swift to WebAssembly (which you can then run in the browser), but also a full-blown browser-based IDE - MiniSwift Studio.
In this article, he explains how the translation process works. This is quite honestly one of the most approachable writings I’ve ever read about how compilers work, kudos!
If you author blog posts or tutorials, MiniSwift should get you very excited - you can now put interactive playgrounds right into your tutorials, and blog posts.
Don’t believe me? Here is a little demo. This is not a screenshot - you can interact with it in your browser. You can even change the code if you want - give it a try:
Oh, and if you’re a Kotlin developer, you’ll be pleased to hear that Ugur also built MiniKotlin - a Kotlin compiler in your browser. Oh, and of course there is an IDE, too.
With Xcode 27, this changes - @State is now a Swift macro. What must be one of the most significant refactorings of @State since it was first introduced doesn’t (necessarily) result in a breaking change at the call site. However, as Nikita points out, one caveat remains - lazy initialization only applies to the property declaration, not the initializer.
SwiftUI sheets support setting different heights using presentationDetents(_:) - but what if you want to resize a sheet to exactly the height of its content?
One of the hidden heroes of Apple’s Foundation Model framework is DynamicProfile. Let’s be honest, it’s not the most exciting name, but in fact this is the key ingredient for building agentic experiences!
Itsuki shows how you can use the lifecycle modifiers (like onActivate(perform:), onToolCall(perform:), onResponse(perform:), and others) to implement features like requesting the user’s permission to run tools.
In Issue 92, I mentioned whenwords - a software library that contained no code. The premise of whenwords is that you write a spec and then let your coding agent build the library in your programming language of choice.
This approach isn’t very token-efficient, and you’d end up re-generating the library every time you run a clean build.
Miguel de Icaza takes a similar “why write code like an animal” approach with AgenticSwift. Instead of writing function bodies manually, you annotate an empty function declaration with a prompt describing what you want.
The macro uses one of several configurable coding agents to generate the function body using this prompt:
private func prompt( for req: AIBodyGenerationRequest) -> String { """ Generate Swift implementation body for this function. Return only Swift statements belonging inside the body. No Markdown, code fences, imports, or explanations. User request: \(req.userPrompt) Function signature: \(req.functionSignature) Attached declaration: \(req.attachedDeclaration) """}
When I first saw Miguel’s approach, I thought it was an April Fool’s joke, but I can see how it can be useful to define a function by specifying its behaviour in plain text. Take this example:
@ai("Return true when text is a \ palindrome. Ignore case, \ spaces, and punctuation.")func isLoosePalindrome( _ text: String) -> Bool
Instead of vibe-coding your entire app, this approach allows you to inject AI-generated code at clearly marked spots in your codebase.
What do you think - did Miguel pull my leg, or is this genuinely useful?
I’ll be honest - I’ve been using AI tools for my coding and other work ever since the first release of ChatGPT in November 2022, and I’ve come to enjoy the benefits of increased productivity.
At the same time, I feel uneasy about my skills as a software developer and author (BTW, all of the words in this newsletter were written manually, even without the support of inline code completion).
A study among physicians who specialise in endoscopy seems to confirm that the use of AI can erode human abilities.
From personal evidence, I agree with the conclusion: to avoid losing your skills, you need to be aware how much you’re offloading to AI, and always maintain your own critical thinking.
What’s your experience? Hit reply and let me know - I read and reply to all emails you send me!
ELIZA is known to be the first chatbot. What most people don’t know is that it’s not a single program, but instead an interpreter that runs a script.
The most famous script is DOCTOR, a simulation of a Rogerian therapist.
For the longest time, the original source code (420 lines of MAD-SLIP) was unavailable, until the Finding Eliza team discovered it in Joseph Weizenbaum’s archive at MIT in 2021 (here is a copy on the Internet Archive).
The Finding Eliza team put together a beautifully designed site that explores ELIZA from different angles. What I really like about this site is that you can explore everything interactively - the DOCTOR script, the interpreter, and of course, ELIZA itself.
I really encourage you to give it a try, it’s fascinating.
If you’re interested in re-creating ELIZA with modern-day LLMs, watch my keynote “From code breaking to code making” which I presented at The National Museum of Computing at Bletchley Park.