Skip to content

Instantly share code, notes, and snippets.

@karwa
karwa / variadic-generics-comparable.swift
Created February 22, 2024 15:53
Varadic property comparison in Swift
public enum SortOrder {
case ascending
case descending
}
@inlinable @inline(__always)
public func isLessThan<Root, each Value: Comparable>(
_ leftRoot: Root,
_ rightRoot: Root,
comparing properties: repeat ((Root) -> each Value, SortOrder)
@karwa
karwa / nested-protocols.md
Created May 31, 2023 17:27
Nested protocols

Allow Protocols to be Nested in Non-Generic Contexts

@karwa
karwa / make_sysroot.py
Last active April 19, 2023 08:32
Creates a linux sysroot with packages required for swift (requires dpkg-deb)
#!/usr/bin/env python
import argparse
import sys
import gzip
import urllib
import urlparse
import posixpath
import cStringIO
import os
#! /usr/bin/swift
//
// - This is just some AppKit boilerplate to launch a window.
//
import AppKit
@available(OSX 10.15, *)
class AppDelegate: NSObject, NSApplicationDelegate {
let window = NSWindow()
let windowDelegate = WindowDelegate()
extension Collection {
public subscript(inplace_slice bounds: Range<Index>) -> Slice<Self> {
get { fatalError() }
_modify {
var slice = Slice(base: self, bounds: bounds)
yield &slice
}
}
}
@karwa
karwa / docc-bootstring.swift
Last active June 14, 2022 12:01
proof of concept bootstring encoding for docc
// Copyright The swift-url Contributors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
@karwa
karwa / AutoFormPOC.swift
Created January 29, 2022 22:49
POC form generation using private reflection APIs
import SwiftUI
struct ContentView: View {
@State var immutableForm = false
@State var data = Person(
name: "Johnny Appleseed", address: "The spaceship", dateOfBirth: Date(),
aSwitch: true, anImmutableString: "Hello, world!", anImmutableSwitch: false
)
var body: some View {
// Use the following Package.swift:
// -----------------------------------------------------------
// swift-tools-version:5.5
// The swift-tools-version declares the minimum version of Swift required to build this package.
//
//import PackageDescription
//
//let package = Package(
// name: "uniqueid-test",
// dependencies: [