Skip to content

Instantly share code, notes, and snippets.

View krzyzanowskim's full-sized avatar

Marcin Krzyzanowski krzyzanowskim

View GitHub Profile
func XCTAssertThrowsError2<T>(_ expression: () throws -> T, _ message: @autoclosure () -> String = "", file: StaticString = #file, line: UInt = #line, _ errorHandler: (Error) -> Void = { _ in }) {
do {
_ = try expression()
XCTFail(message())
} catch {
errorHandler(error)
}
}
@krzyzanowskim
krzyzanowskim / ambiguous.swift
Created June 11, 2019 21:58
throwing closure parameter results in different resolution
class A {}
// foo
func foo<T>(_ v: T, handler: () -> Void) -> T {
print("regular")
return v
}
func foo<T>(_ v: T?, handler: () throws -> Void) rethrows -> T? {
@krzyzanowskim
krzyzanowskim / crash.swift
Last active June 10, 2019 10:18
swift-DEVELOPMENT-SNAPSHOT-2019-06-02
// https://bugs.swift.org/browse/SR-10906
import Foundation
protocol ViewDataSource: class {
func foo<T>() -> [T]
}
class View {
weak var delegate: ViewDataSource?
@krzyzanowskim
krzyzanowskim / didSet_Array.swift
Created June 1, 2019 21:48
didSet is called on each update of Array
class Node: CustomStringConvertible {
let title: String
var parent: Node?
var children: [Node] = [] {
didSet {
children.forEach({ $0.parent = self })
}
}
var description: String {
@discardableResult
public func with<T>(_ value: T, _ builder: (T) -> Void) -> T {
builder(value)
return value
}
@discardableResult
public func with<T>(_ value: T, _ builder: (T) throws -> Void ) rethrows -> T {
try builder(value)
return value
import Cocoa
final class WindowController: NSWindowController {
init() {
let window = NSWindow()
window.styleMask = [.closable, .miniaturizable, .resizable, .titled]
super.init(window: window)
self.shouldCascadeWindows = false
window.setFrameAutosaveName(String(describing: type(of:self)))
import Foundation
public class Foo: Por {
public required init() {}
}
public protocol Por {
associatedtype As = Void
init(parameters: As?) throws
}
/*
* Optional value is not initialized with nil, instead is initialized with 0 value
*/
let p = UnsafeMutablePointer<UInt8?>.allocate(capacity: 0)
// p.initialize(repeating: nil, count: 1)
print("Value Type: \(type(of:p.pointee))")
if let value = p.pointee {
import Foundation
// Process stream
// http://ascii-table.com/ansi-escape-sequences-vt-100.php
// http://ascii-table.com/ansi-escape-sequences.php
public final class ANSIEscapeProcessor {
public func tokens(string: String) -> [Token] {
var tokens: [Token] = []
Load command 12
cmd LC_LOAD_DYLIB
cmdsize 80
name @executable_path/../Frameworks/libSwiftSyntax.dylib (offset 24)
time stamp 2 Thu Jan 1 01:00:02 1970
current version 0.0.0
compatibility version 0.0.0
Load command 43
cmd LC_RPATH
cmdsize 32