Skip to content

Instantly share code, notes, and snippets.

import Equatable
@Equatable
struct SomeStruct { }
let a = SomeStruct()
let b = SomeStruct()
print(a == b)
@griotspeak
griotspeak / update-swift-dev
Created May 18, 2017 02:12 — forked from CodaFi/update-swift-dev
This is the script I currently use on OS X to get a working "swift-dev.xctoolchain" out of a built Swift. It isn't designed to work for anyone but me, but it should be easy to adapt. I always run this after every build, and then use `TOOLCHAINS=swift-dev swift build` (etc) to use the development compiler.
#!/bin/sh
####################################################
# HEY! set SWIFT_SOURCE_PATH or nothing will work. #
####################################################
set -e
if [ -z "${SWIFTPM_CONFIGURATION}" ]; then
SWIFTPM_CONFIGURATION=lib/swift/pm/4
@griotspeak
griotspeak / PureFunctionsProposal.md
Last active February 17, 2017 18:37
Pure Functions

Pure Functions

  • Proposal: SE-NNNN
  • Author(s): TJ Usiyan
  • Status: Awaiting review
  • Review manager: TBD

Introduction

Some functions are, essentially, only meant to be transformations of their input and–as such–do not and should not reference any variables other than those passed in. These same functions are not meant to have any effects other than the aforementioned transformation of input. Currently, Swift cannot assist the developer and confirm that any given function is one of these 'pure' functions. To facilitate this, this proposal adds syntax to signal that a function is 'pure'.

@griotspeak
griotspeak / JSONConvenience.swift
Created October 2, 2016 03:30
Convenience overload for JSON parsing
import Foundation
extension JSONSerialization {
enum CastingError : Swift.Error {
case incompatibleType
}
static func jsonObject<T>(with data: Data, options: JSONSerialization.ReadingOptions) throws -> T {
let anyResult: Any = try jsonObject(with: data, options: options)
if let result = anyResult as? T {
@griotspeak
griotspeak / Settings.swift
Created September 21, 2016 01:31 — forked from anonymous/Settings.swift
SwiftInSwift - A poor man's meta programming tool for Swift
//-----------------------------------------------------------------------------
// Settings.swift
// SwiftInSwift
//-----------------------------------------------------------------------------
let appName = "SwiftInSwift"
let defBlockPrefix = "// DEF-{"
let defBlockSuffix = "// }-DEF"
@griotspeak
griotspeak / GalacticKnight.h
Created August 17, 2016 19:10
Designated initializers
//
// GalacticKnight.h
// MoreMessage
//
// Created by TJ Usiyan on 2016/17/08.
// Copyright © 2016 Buttons and Lights LLC. All rights reserved.
//
#import <Foundation/Foundation.h>
#import "Person.h"
@griotspeak
griotspeak / EnumSubsets.md
Last active January 12, 2018 23:36
Enum Subsets

This is loosely related to but not meant to 'compete' with the ad hoc enum proposal.

Introduction

This proposal adds/creates syntax to allow ad hoc creation of enums whose members are strict subsets of explicitly defined enums.

Swift-evolution thread: Discussion thread topic for that proposal

Motivation

Consider a situation where we have an enum Color which represents the entire set of colors relevant to your application with many salient methods and operations. We have also declared an enum LCDColorModel with only three colors, red, blue, green .

@griotspeak
griotspeak / LiteralHint.swift
Last active May 28, 2016 23:03
Wish I could tell the compiler to just use Expression
struct Expression : IntegerLiteralConvertible {
let value: Int
init(integerLiteral value: Int) {
self.value = value
}
}
struct Subexpression : IntegerLiteralConvertible {
let value: Int
@griotspeak
griotspeak / Int.swift
Last active May 23, 2016 16:29
Divmod Int.swift
extension Int {
internal func divMod(other:Int) -> (quotient:Int, modulus:Int) {
let quotient = self / other
let remainder = self % other
if (quotient > 0) || (remainder == 0) {
return (quotient, remainder)
} else if quotient == 0 && (self > 0) && (other < 0) {
let div = quotient - 1
let result = (div * other) - self
@griotspeak
griotspeak / Int.swift
Last active May 23, 2016 16:26
DivMod
extension Int {
internal func divMod(other:Int) -> (quotient:Int, modulus:Int) {
let quotient = self / other
let remainder = self % other
if (quotient > 0) || (remainder == 0) {
return (quotient, remainder)
} else if quotient == 0 && (self > 0) && (other < 0) {
let div = quotient - 1
let result = (div * other) - self