Skip to content

Instantly share code, notes, and snippets.

View guidomb's full-sized avatar

Guido Marucci Blas guidomb

View GitHub Profile
@guidomb
guidomb / gist:063aca6a97b557598025b20fed1ffe6c
Last active June 3, 2018 03:22
Swift compiler crasher dump - only on linux - swif 4.2
Swift version: Swift version 4.2-dev (LLVM c41d146806, Clang 3ef62dacf4, Swift 57940b7c68)
Target: x86_64-unknown-linux-gnu
swift: /home/buildnode/jenkins/workspace/oss-swift-4.2-package-linux-ubuntu-16_04/swift/lib/SILGen/ManagedValue.cpp:113: void swift::Lowering::ManagedValue::forwardInto(swift::Lowering::SILGenFunction &, swift::SILLocation, swift::SILValue): Assertion `isPlusOne(SGF)' failed.
/usr/bin/swift[0x40fc344]
/usr/bin/swift[0x40fa1d2]
/usr/bin/swift[0x40fc4f2]
/lib/x86_64-linux-gnu/libpthread.so.0(+0x11390)[0x7fcccbb0f390]
/lib/x86_64-linux-gnu/libc.so.6(gsignal+0x38)[0x7fccca24e428]
/lib/x86_64-linux-gnu/libc.so.6(abort+0x16a)[0x7fccca25002a]
@guidomb
guidomb / condconform.swift
Last active April 1, 2018 05:29
Conditional conformace compiler bug?
public protocol QueryStringConvertible {
var asQueryString: String { get }
}
extension Array: QueryStringConvertible where Element: QueryStringConvertible {
public var asQueryString: String {
return self.reduce("") { $0.isEmpty ? $1.asQueryString : "\($0)&\($1.asQueryString)" }
@guidomb
guidomb / MirrorSerialize.swift
Created March 25, 2018 00:14
A generic serialization function in Swift using Mirror API
func serialize(object: Any) -> [String : Any]? {
let mirror = Mirror(reflecting: object)
guard mirror.displayStyle == .struct || mirror.displayStyle == .class else {
return .none
}
var result: [String : Any] = [:]
for case let (label?, value) in mirror.children {
let childMirror = Mirror(reflecting: value)
if let displayStyle = childMirror.displayStyle {
import Foundation
import Darwin
if CommandLine.argc < 2 {
print("input file missing")
exit(1)
}
if CommandLine.argc < 3 {
print("column name missing")
exit(1)
@guidomb
guidomb / maybecompilerbug.swift
Created September 25, 2017 23:25
Example variations of code that may reproduce a Swift compiler bug
// This compiles
let refreshState: RefreshState<String> = .searching
let refreshProperties: RefreshProperties<String> = properties(state: refreshState) {
print("")
$0.title = NSAttributedString(string: "Collection refresh")
}
print(refreshProperties)
// This compiles
let refreshState: RefreshState<String> = .searching
@guidomb
guidomb / CustomTypeConversion.swift
Created September 8, 2017 15:03
An example of how I would like custom type conversion to work.
let foo = ZipList(left: [1, 2, 3], center: 4, right: [5, 6, 7])
// This is what I would like to have, a custom type conversion / casting
// ([Element], Element, [Element]) => ZipList<Element>
let bar: ZipList<Int> = ([1, 2, 3], 4, [5, 6, 7])
// ZipList type definition
public struct ZipList<Element>: Collection, CustomDebugStringConvertible {
public var startIndex: Int {
@guidomb
guidomb / integration.rb
Created July 18, 2017 23:11
An example of an integration using a Ruby DSL
event_bus.calendar.new_event do |event|
# Schedule a task to be executed once the calendar event
# has finished
schedule_task(on: event.end_date) do |date|
# Utility function that gets a Logger::Activity
# object from the calendar event. It tries to
# infer it based on the metada of the event
activity = logger.activity(for: event)
event.participants.each do |participant|
slack_user = find_slack_user(email: participant.email)
@guidomb
guidomb / identifier.swift
Created July 13, 2017 13:39
Generic type safe object identifier in Swift
struct Identifier<ObjectType>: RawRepresentable, Hashable, Equatable {
let rawValue: String
init(rawValue: String) {
self.rawValue = rawValue
}
}
@guidomb
guidomb / example.swift
Created June 3, 2017 14:00
Swift constrained extension where fixed type is generic
public protocol Route: Equatable {
var previous: Self? { get }
}
internal enum InternalAction<RouteType: Route, MessageType> {
case navigateToPreviousRouteAfterPop
case action(Action<RouteType, MessageType>)
extension Sequence {
func filter<A, B>(_ isIncluded: (A, B) -> Bool) -> Self {
return self.filter { isIncluded($0.0, $0.1) }
}
func filter<A, B, C>(_ isIncluded: (A, B, C) -> Bool) -> Self {
return self.filter { isIncluded($0.0, $0.1, $0.2) }
}