Skip to content

Instantly share code, notes, and snippets.

View anandabits's full-sized avatar

Matthew Johnson anandabits

View GitHub Profile
@anandabits
anandabits / list-overlay-clipping.swift
Last active November 17, 2020 17:35
SwiftUI List overlay clipping
// NOTE: behavior is exhibited in the 14.1 simulator and previews in Xcode 12.1
struct OverlayRowClipping: View {
let rowData = (1...50).map { "row \($0)" }
@State var alertIsOpen = false
var body: some View {
List {
Section(header: Text("The Header")) {
@anandabits
anandabits / Identifiable.md
Last active July 2, 2019 23:45
Identifiable proposal

Identifiable

Introduction

SwiftUI introduces an Identifiable protocol. This concept is broadly useful—

@anandabits
anandabits / Catamorphism.swift
Created June 27, 2019 18:16
An implementation of the catamorphism recursion scheme in Swift
// NOTE: This code was written when I first figured out how to encode HKT in Swift.
// There is a lot that can be improved and I would write it somewhat differently today.
// This example shows how higher-kinded types can be emulated in Swift today.
// It acheives correct typing at the cost of some boilerplate, manual lifting and an existential representation.
// The technique below was directly inspired by the paper Lightweight Higher-Kinded Polymorphism
// by Jeremy Yallop and Leo White found at http://ocamllabs.io/higher/lightweight-higher-kinded-polymorphism.pdf
/// `ConstructorTag` represents a type constructor.
/// `Argument` represents an argument to the type constructor.
@anandabits
anandabits / GADT.swift
Created November 26, 2017 04:23
Emulating GADT in Swift
/*
If you're willing to write a little bit of boilerplate you can have type-safe GADT in Swift today.
This is accomplished using a wrapper struct with a phantom type parameter that wraps a private enum value.
Static factory methods on the struct wrap each case returning a value with the phantom type bound as necessary.
An extension is created for each phantom type binding providng a `switch` method that requires each case
with a matching type to be covered and uses `fatalError` for cases where values will never be created.
New members are added in extensions that bind the phantom type and make use of the `switch` method.
The example below is drawn from https://en.m.wikibooks.org/wiki/Haskell/GADT
*/
@anandabits
anandabits / enhanced-variadic-parameters.md
Last active April 11, 2019 02:31
Enhanced Variadic Parameters

Enhanced Variadic Parameters

Introduction

This proposal enhances variadic parameters by introducing support for user-defined types, labeled variadic parameters, and a choice at the call site of whether to provide a variadic argument list or a collection value.

@anandabits
anandabits / Monoid.swift
Created January 8, 2019 00:24
Exploring the design space for the Monoid abstraction in Swift
/*
This gist contains an analysis of the design space for abstractions in a type system like Swift's.
It focuses on Monoid as an example but the same set of tradeoffs apply to any abstraction.
As such, it focuses on a single abstraction and does not explore issues that arise when designing
an abstraction hierarchy in depth.
The matrix below describes some of the design important design tradeoffs for various approaches including:
- OO style protocols such as `Monoid { var identity: Self ... }
(Haskell also adopts this approach for many abstractions, including for Monoid)
- ML signature style static protocols with empty enum conformances analagous with ML structures
@anandabits
anandabits / explicit-memberwise-initializers.md
Last active April 10, 2019 02:53
Explicit Memberwise Initializers

Explicit Memberwise Initialization

Introduction

@anandabits
anandabits / ConstraintAbstraction.swift
Created February 12, 2018 15:46
A (non-working) experiment in abstracting over constraints in Swift
// NOTE: This is an experiement that does not actually work (in the 2/8/18 nightly toolchain).
// The idea is to use Swift's constraint inference to be able to abstract an arbitrary set of constraints.
/// An empty enum serving as an example of the general case of
/// representing a set of constraints that relate multiple types.
/// This specific example provides the constraint that both types are sequences and they have the same Element type.
enum Parallel<S1: Sequence, S2: Sequence> where S1.Element == S2.Element {
typealias First = S1
typealias Second = S2
}
@anandabits
anandabits / literal-syntax-protocols.md
Last active June 24, 2016 15:27
Literal Syntax Protocols

Literal Syntax Protocols

Introduction

This proposal renames the *LiteralConvertible protocols to Syntax.*Literal.