Skip to content

Instantly share code, notes, and snippets.

View markmals's full-sized avatar

Mark Malstrom markmals

View GitHub Profile
@DougGregor
DougGregor / macros.md
Last active October 24, 2023 16:42
A possible vision for macros in Swift

A Possible Vision for Macros in Swift

As Swift evolves, it gains new language features and capabilities. There are different categories of features: some fill in gaps, taking existing syntax that is not permitted and giving it a semantics that fit well with the existing language, with features like conditional conformance or allowing existential values for protocols with Self or associated type requirements. Others introduce new capabilities or paradigms to the language, such as the addition of concurrency or comprehensive reflection.

There is another large category of language features that provide syntactic sugar to eliminate common boilerplate, taking something that can be written out in long-form and making it more concise. Such features don't technically add any expressive power to the language, because you can always write the long-form version, but their effect can be transformational if it enables use cases that would otherwise have been unwieldy. The synthesis of Codable conformances, for ex

@christianselig
christianselig / swiftui-magic-number-helper.md
Last active July 27, 2022 21:24
Make magic numbers more accessible in SwiftUI

More Accessible Magic Numbers

A lot of StackOverflow answers for dealing with things in SwiftUI sometimes being positioned not how you want (for example) understandably solve it with a magic number to put something where they want.

The downside of this is that it likely only works at the default Dynamic Type size, so anyone with larger or smaller text settings (for accessibility reasons, for instance) will likely have a layout that doesn't look as optimized (for instance, if you assume 24.0 is a good number based on the default .large size, if they have their phone set to accessibilityExtraExtraExtraLarge, 24.0 will likely be way too small).

So here's a quick helper extension that simply scales a number based on the Dynamic Type size, so that 24.0 will increase and decrease with the user's Dynamic Type setting.

// Extending BinaryFloatingPoint so that this ext
@NSExceptional
NSExceptional / Shell.swift
Created June 25, 2022 23:31
Leverage @dynamicMemberLookup to invoke shell commands dynamically
//
// Shell.swift
//
// Created by Tanner Bennett on 6/25/22.
// Copyright Tanner Bennett (c) 2022
//
import Foundation
extension StringProtocol {
@mattmassicotte
mattmassicotte / AsyncMap.swift
Created June 18, 2022 11:00
Swift asyncMap function
extension Sequence {
func asyncMap<T>(_ transform: @escaping (Element) async -> T) async -> [T] {
return await withTaskGroup(of: T.self) { group in
var transformedElements = [T]()
for element in self {
group.addTask {
return await transform(element)
}
}
// I learned about `#if canImport(module, _version: x.y)` from Tony Allevato:
// https://forums.swift.org/t/pitch-sdk-conditional-code/52642/4
//
// Does this work reliably? I have no idea!
// I have only done some quick experiments in a playground.
//
// The Foundation version in Xcode 14.0b1 is:
// - in iOS's Foundation.swiftinterface: 1932.104
// - in macOS's Foundation.swiftinterface: 1932.401
//
@phranck
phranck / PlatformVisibility.swift
Last active August 30, 2024 00:05
A Swift view modifier to handle visibility of views for specific platforms
import SwiftUI
public struct Platform: OptionSet {
public var rawValue: UInt8
public static let iOS = Platform(rawValue: 1 << 0)
public static let macOS = Platform(rawValue: 1 << 1)
public static let tvOS = Platform(rawValue: 1 << 2)
public static let watchOS = Platform(rawValue: 1 << 3)
public static let all: Platform = [.iOS, .macOS, .tvOS, .watchOS]
@ole
ole / HeterogeneousDictionary.swift
Last active January 23, 2025 20:54
Code for my article "A heterogeneous dictionary with strong types in Swift" https://oleb.net/2022/heterogeneous-dictionary/
// A heterogeneous dictionary with strong types in Swift, https://oleb.net/2022/heterogeneous-dictionary/
// Ole Begemann, April 2022
/// A key in a `HeterogeneousDictionary`.
public protocol HeterogeneousDictionaryKey {
/// The "namespace" the key belongs to. Every `HeterogeneousDictionary` has its associated domain,
/// and only keys belonging to that domain can be stored in the dictionary.
associatedtype Domain
/// The type of the values that can be stored under this key in the dictionary.
associatedtype Value
@christianselig
christianselig / swift-async-await-example.swift
Created February 28, 2022 20:46
Some questions about async await threading in Swift's new concurrency model.
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
Task {
// 1️⃣❓ UIViewController is in a MainActor context, so this Task
// will inherit that, so the following pretend expensive call will
// be on the main thread and likely block?
ExpensiveOperationPerformer.doExpensiveLoopAndPrint()
}
import ReactDOM from "react-dom";
import { RemixBrowser } from "remix";
// hydrate a fake Document with the patch:
ReactDOM.hydrate(<RemixBrowser />, {
childNodes: [document.documentElement],
firstChild: document.documentElement,
appendChild(n) { document.replaceChild(n, document.documentElement); },
insertBefore(n) { this.appendChild(n); }
});
@ryansolid
ryansolid / Comparison - Todo Sizes.md
Last active March 19, 2025 19:21
Looking at how frameworks scale using their official TodoMVC Example and Vite

Based on Evan You's comparison which included Svelte and Vue. https://github.com/yyx990803/vue-svelte-size-analysis

For Preact, React, and Solid I took their official TodoMVC and ran them through Vite (2.3.6) to get vendor. For the components I grabbed the unminified source, ran it through Terser Repl, removed the imports, and ran through Brotli.

I wanted to use hooks for React and Preact since those are much smaller but all the official demos use classes. Adding Hooks adds library weight to Preact but it is worth it for this comparison(Preact with classes was 1.60kb and 3.81kb). Honestly I couldn't find a good hooks implementation for React/Preact that was small so I made one myself based on Solid.

Preact React Solid Svelte Vue
component size (brotli) 1.21kb 1.23kb 1.26kb 1.88kb 1.10kb