Creating DSLs using F#’s Computation Expressions

This article is part of the 2022 F# Advent Calendar. Go check out the other awesome posts that are part of it here! As a user of F# I’ve been using lots of seq { } and async { } computation expressions, however I never really wrote my own. Lately I’ve been getting more and more into making DSLs and I found CEs to be an absolutely god-send for this.

Swift formatters

The easiest way to get a formatted element is to use the formatted method that is available on a myriad of data types without the need to instantiate our own formatter. And the best thing is that all these things are also available directly on SwiftUI controls like Text and TextField by using the format parameter. Below are the most common/useful ones I’ve found so far. Dates In order to get a date formatted, we can simply call formatted on a Date type, which gives us the full, long formatted date:

Grouped queries in SwiftUI

Querying While developing Maby I needed to retrieve all events that happened, grouped by dates. Now of course I could just slap a FetchRequest and then manually group the results into a dictionary, specially given how easy it is with the grouping init, but it turns out that there’s a better way. Since iOS/iPadOS/tvOS 15, macOS 12 and watchOS 8 there’s a new fetch request in town called SectionedFetchRequest that allows us to specify a type to group by and an entity to group, so for example in my case I could just do the following:

Unrecognized selector objectAtIndex

Unrecognized selector objectAtIndex 2022-06-21 22:41:30.447763+0200 App[56115:1179524] -[__NSCFSet objectAtIndex:]: unrecognized selector sent to instance 0x600003df9c20 This happened with the following code, basically when trying to do a ForEach on a property coming from a CoreData model: import CoreData import SwiftUI struct BudgetView: View { @FetchRequest(entity: CategoryGroup.entity(), sortDescriptors: []) var categoryGroups: FetchedResults<CategoryGroup> var body: some View { VStack { List { ForEach(categoryGroups, id: .id) { group in Text( ForEach(group.categories, id: .id) { category in Text(category.

Hello world!

Are we live? Mic check, mic check. So I’m just starting to re-learn Swift and SwiftUI and since Apple’s documentation is lacking to say the least I’ve decided to start writing this little blog to keep everything I’m finding. The idea is to write short articles straight to the point on how to do X and Y. Sort of like a “knowledge graph” but without the graph 🙂