Skip to content

Instantly share code, notes, and snippets.

View austinzheng's full-sized avatar

Austin Zheng austinzheng

View GitHub Profile
@austinzheng
austinzheng / genericsDeadlock.swift
Last active August 29, 2015 14:14
generics deadlock example
import Foundation
// Build this as an OS X command line project, then run it. It should never terminate.
enum Value<T> {
case Nil
case Cons(T, LinkedList<T>)
}
final class LinkedList<T> {
@austinzheng
austinzheng / prepostseq.swift
Created February 7, 2015 09:27
prefixed postfixed sequence
// Released under the terms of the MIT license.
struct PrefixedPostfixedSequence<T : SequenceType, U where U == T.Generator.Element> : SequenceType {
private let sequence : T
private let initial : U?
private let final : U?
init(_ sequence: T, initial: U? = nil, final: U? = nil) {
self.sequence = sequence
self.initial = initial
@austinzheng
austinzheng / CustomTextInputView.swift
Last active January 20, 2022 15:14
Simple UIKeyInput example
import UIKit
/// A very simple example view that can accept keyboard input and add or delete text from an enclosed label.
class CustomTextInputView : UIControl, UIKeyInput {
var label : UILabel?
override func canBecomeFirstResponder() -> Bool {
return true
}
@austinzheng
austinzheng / funcLabels.swift
Last active August 29, 2015 14:23
Weird stuff with free function labels in Xcode 7.0 beta 2...
import UIKit
// Running on Xcode 7.0 beta (7A121l)
// All running in a playground
// Open console (View -> Debug Area -> Show Debug Area)
// Define a non-generic free fn taking 1 arg
func notGeneric(x: String) {
print("notGeneric called")
}
@austinzheng
austinzheng / swiftDelegateExample.swift
Created August 1, 2015 00:12
A simple example of setting up a delegate in Swift.
//
// ExampleCode.swift
//
import UIKit
// MARK: - Protocol
protocol SearchQueryProviderProtocol : class { // 'class' means only class types can implement it
func searchQueryData() -> String
PROMOTION DEMOTION
(init?) (init)
[~~~~~~~~~~~AnyRandomAccessCollection~~~~~~~~~~~]
^ ^ | |
| | | |
| | v |
| [~~AnyBidirectionalCollection~~] |
| ^ | |
| | | |
| | v v
import Foundation
func main() {
print("Starting")
// GCD works by dispatching chunks of code (represented by closures, and called 'blocks') onto things called 'dispatch
// queues'. These queues run the blocks, either on the main thread or in a background thread. (Queues don't correspond
// to threads on a one-to-one basis; GCD is responsible for spinning up threads to service queues.)
// Here's a variable representing shared state I want to manipulate from different threads.
@austinzheng
austinzheng / generics_manifesto.md
Last active October 23, 2023 20:38
Douglas Gregor's Swift Generics Manifesto, with added markdown formatting

(Source: https://lists.swift.org/pipermail/swift-evolution/Week-of-Mon-20160229/011666.html)

Introduction

The “Complete Generics” goal for Swift 3 has been fairly ill-defined thus fair, with just this short blurb in the list of goals:

Complete generics: Generics are used pervasively in a number of Swift libraries, especially the standard library. However, there are a number of generics features the standard library requires to fully realize its vision, including recursive protocol constraints, the ability to make a constrained extension conform to a new protocol (i.e., an array of Equatable elements is Equatable), and so on. Swift 3.0 should provide those generics features needed by the standard library, because they affect the standard library's ABI.

This message expands upon the notion of “completing generics”. It is not a plan for Swift 3, nor an official core team communication, but it collects the results of numerous discussions among the core team and Swift developers, both of the compiler an

func compare(a: Any, b: Any) -> Bool {
if let a = a as? Equatable, a openas T {
if let b = b as? Equatable, b openas T {
return a == b
}
}
fatalError("a and b aren't comparable")
}
@austinzheng
austinzheng / swift-kvc.swift
Last active May 27, 2016 03:44
An exploration of a possible KVC-like API for a future version of Swift.
/// A statically typed view into a get-only property.
struct TypedGetPropertyView<T> {
/// The name of the property, as a string.
let name : String
/// The actual metatype of the property.
let metatype : T.Type
/// Get the value of the property.
func get() -> T