Skip to content

Instantly share code, notes, and snippets.

View allevato's full-sized avatar

Tony Allevato allevato

View GitHub Profile
@allevato
allevato / ExistentialOperations.swift
Created April 6, 2022 03:43
How to compare two Anys for equality in Swift
/// Returns a Boolean value indicating whether the two arguments are equal according to their
/// conformance to `Equatable`.
///
/// If `lhs` and `rhs` are the same type and that type conforms to ``Equatable``, then this function
/// returns the result of evaluating `==` on those values. Otherwise (if the values are different
/// types, or the same type but the type does not conform to `Equatable`), this function returns
/// false.
func areValuesEqual(_ lhs: Any, _ rhs: Any) -> Bool {
func isOpenedValueEqual<LHS>(to openedLHS: LHS) -> Bool {
let equatable = ExistentialOperations<LHS>.self as? ExistentialEquatable.Type
@allevato
allevato / Transient.swift
Created September 12, 2017 14:21
Wrapper for transient properties to use with synthesized protocol defaults
/// Wraps a value but causes it to be treated as "value-less" for the purposes
/// of automatic Equatable, Hashable, and Codable synthesis. This allows one to
/// declare a "cache"-like property in a value type without giving up the rest
/// of the benefits of synthesis.
public enum Transient<Wrapped>: Equatable, Hashable, Codable {
case none
case some(Wrapped)
public static func == (lhs: Transient<Wrapped>, rhs: Transient<Wrapped>) -> Bool {
@allevato
allevato / Flip.swift
Created October 20, 2016 21:36
FLIP THAT TABLE
prefix operator ╯°□°╯┻━┻
prefix func ╯°□°╯┻━┻ (s: String) -> String {
return String(s.characters.reversed())
}
print(╯°□°╯┻━┻"hello world") // "dlrow olleh"