Skip to content

Instantly share code, notes, and snippets.

SE-0413: Typed Throws

1. Core Semantics

The throws(T) syntax restricts a function to throwing a specific concrete error type T, which must conform to the Error protocol. The legacy throws keyword is preserved as syntactic sugar equivalent to throws(any Error), maintaining backward compatibility while allowing for precise error contracts.

2. Never & Uniformity

A function defined as throws(Never) is semantically identical to a non-throwing function, allowing the compiler to optimize away error handling paths. This unifies the type system, enabling generic algorithms to accept both throwing and non-throwing closures without requiring separate overloads.

@manmal
manmal / ClockDriftDetectingStream.swift
Created June 24, 2025 08:38
ClockDriftDetectingStream - needs to "open" clock existential
/// `some` opens the existential clock and instants
func clockDriftDetectingStream(
_ clock: some Clock<Duration>,
checkEverySeconds: Double,
toleranceSeconds: Double
) -> AsyncStream<Void> {
let startInstant = clock.now
let period = Duration.seconds(checkEverySeconds)
let tolerance = Duration.seconds(toleranceSeconds)
@manmal
manmal / invert_pinboard_export.py
Last active January 26, 2025 18:07
Invert Pinboard HTML Backup for import into Linkwarden
from bs4 import BeautifulSoup
# Run:
# $ pip install beautifulsoup4
# For more info: https://github.com/linkwarden/linkwarden/issues/555
INPUT_FILE = "pinboard_export.html"
def reverse_bookmarks():
@manmal
manmal / ResultAsyncExtensions.swift
Created August 28, 2024 20:10
ResultAsyncExtensions
import Foundation
extension Result {
public func flatMap<NewSuccess>(
_ transform: (Success) async -> Result<NewSuccess, Failure>
) async -> Result<NewSuccess, Failure> {
switch self {
case let .success(success):
await transform(success)
case let .failure(error):
@manmal
manmal / ScrollViewThatFits.swift
Last active March 1, 2024 16:08
Wrap a SwiftUI View in a ScrollView, expanding to full height/width & preventing bouncing when possible (iOS > = 16.4)
import SwiftUI
/// Wraps the content in a ScrollView that only bounces
/// if the content size exceeds the visible area;
/// and makes the content fill the ScrollView if desired.
/// Per default, the content fills the ScrollView only
/// vertically.
///
/// Usage:
///
@manmal
manmal / AsyncPipe.swift
Created November 24, 2022 14:55
Swift AsyncPipe (send values synchronously into an AsyncStream)
// MIT License
//
// Copyright (c) 2022 Manuel Maly
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
@manmal
manmal / AsyncPassthroughSubject.swift
Created July 17, 2022 09:44
AsyncPassthroughSubject - Swift Structured Concurrency
import Foundation
/// Provides a safe stream of `values`.
public struct AsyncPassthroughSubject<T> {
public let values: AsyncStream<T>
private let input: AsyncStream<T>.Continuation
private var isFinished = false
public init(
bufferingPolicy: AsyncStream<T>.Continuation.BufferingPolicy = .unbounded
@manmal
manmal / TaskAutoCancellation.swift
Last active July 27, 2022 20:52
Swift Structured Concurrency - Task Auto Cancellation
public extension Task {
/// Cancels this `Task` when the surrounding `Task` is cancelled.
/// This is necessary if `Task {}` and `Task.detached {}`
/// should be automatically cancelled - otherwise, such Tasks
/// just run until finished.
///
/// Usage:
///
/// await Task { await myAsyncFunc() }.autoCancel()
func autoCancel() async -> Void {
@manmal
manmal / AsyncSequenceExtensions.swift
Created July 6, 2022 16:57
AsyncSequence.eraseToAsyncStream()
import Foundation
// Props to @pteasima and @MichalKleinJr:
// https://twitter.com/pteasima/status/1544723987606929408?s=21&t=JL1oIuL87Ms_VPBQBZQ7Rg
public extension AsyncSequence {
func eraseToAsyncStream() -> AsyncStream<Element> {
return AsyncStream { continuation in
let task = Task {
do {
for try await value in self {
@manmal
manmal / content.txt
Created June 12, 2022 10:11
FB10198317 - Swift Charts: AxisValueLabel color does not change
When setting `foregroundStyle` on a `AxisValueLabel`, the rendered text color does not change.
Example Code:
```
Chart([(0,0), (1,1), (2, 2), (3, 3), (4, 4)], id: \.0) { point in
LineMark(x: .value("X", point.0), y: .value("Y", point.1))
}
.chartYAxis {
AxisMarks { _ in