Skip to content

Instantly share code, notes, and snippets.

Remove type inference for stored properties

Introduction

When a type's stored property is declared with a default value, user may choose to let Swift's compiler to automatically deduct type for this property instead of writing it explicitly. We propose this feature be removed from Swift.

@dduan
dduan / runCommand.swift
Last active April 1, 2023 16:17
How to fork()+execv() in Swift
import Foundation
func withCStrings(_ strings: [String], scoped: ([UnsafeMutablePointer<CChar>?]) throws -> Void) rethrows {
let cStrings = strings.map { strdup($0) }
try scoped(cStrings + [nil])
cStrings.forEach { free($0) }
}
enum RunCommandError: Error {
case WaitPIDError
@dduan
dduan / withCStrings.swift
Last active August 27, 2018 20:19
A function that turns a Swift 3 [String] to an array of C strings (char *[]).
func withCStrings(_ strings: [String], scoped: ([UnsafeMutablePointer<CChar>?]) throws -> Void) rethrows {
let cStrings = strings.map { strdup($0) }
try scoped(cStrings + [nil])
cStrings.forEach { free($0) }
}
@dduan
dduan / XCTest+Eventually.swift
Last active January 11, 2023 00:26
A simple "eventually" method to aide asynchronous testing with XCTest
import XCTest
extension XCTestCase {
/// Simple helper for asynchronous testing.
/// Usage in XCTestCase method:
/// func testSomething() {
/// doAsyncThings()
/// eventually {
/// /* XCTAssert goes here... */
@dduan
dduan / IBInspectableTest.swift
Created November 26, 2016 06:12
Tests for supported IBInspectable types on iOS
import UIKit
class TestView: UIView {
@IBInspectable private var string: String = ""
@IBInspectable private var stringiuo: String!
@IBInspectable private var stringoptional: String?
@IBInspectable private var iuostring: ImplicitlyUnwrappedOptional<String>
@IBInspectable private var optionalstring: Optional<String>
@IBInspectable private var nsstring: NSString = ""
@IBInspectable private var nsstringiuo: NSString!
@dduan
dduan / migrate.py
Created October 25, 2016 00:10
Migrate Xcode 8.0 style comment to Xcode 8.1 style
# usage:
# IFS=$'\n' find ROOT_SOURCE_FOLDER -name "*.swift" -exec python PATH/TO/migrate.py {} \;
import sys
import re
func = re.compile('func\s+\w+\(')
param = re.compile('(?P<indent>\s*)/// -[ ]*[pP]arameter[ ]+(?P<name>\w+)[ ]*:(?P<description>[^\n]+)\n')
ret = re.compile('(?P<indent>\s*)/// -[ ]*[rR]eturns[ ]*:(?P<description>[^\n]+)\n')
continuation = re.compile('\s*///(?P<description>[^\n]+)')
@dduan
dduan / OneArray.swift
Created August 12, 2016 20:19
An array that's never empty.
enum OneArray<T> {
case one(T)
case many([T])
// Want empty value? Fuck you.
init?<S: Sequence where S.Iterator.Element == T>(_ s: S) {
switch s.underestimatedCount {
case 0:
return nil
case 1:
@dduan
dduan / not-compiling-swift2.swift
Created June 23, 2016 18:15
Will this compile?
protocol X {
var value: Int { get }
init(value: Int)
}
func foo<T, U: X>(a: T) -> U {
return U(value: a.value)
}
@dduan
dduan / blah.py
Last active January 14, 2017 01:02
A script that generate staged file change summaries to be used as temporary commit messages.
#!/usr/bin/env python
# -*- coding: utf-8 -*-
'''Print cached git diff stats in less than 50 characters.
This script takes output from `git diff --cached --stat` and generates a single
line summary with less than 50 characters. This output may be used as a
(hopefully) temporary commit message like so
git commit -m "`blah.py`"
@dduan
dduan / simple-made-easy.sh
Last active March 22, 2018 19:19
Downloading "Simple Made Easy" slides.
#!/bin/sh
mkdir simple-made-easy
cd simple-made-easy
for i in `seq 1 39`
do
wget "https://res.infoq.com/presentations/Simple-Made-Easy/en/slides/sl$i.jpg"
done
for i in `seq 1 9`
do mv sl$i.jpg sl0$i.jpg
done