Skip to content

Instantly share code, notes, and snippets.

@jepers
Last active September 28, 2015 10:34
Show Gist options
  • Save jepers/cb72b7f5432e80130281 to your computer and use it in GitHub Desktop.
Save jepers/cb72b7f5432e80130281 to your computer and use it in GitHub Desktop.
A compile time / type level NAND "function" in Swift.
protocol Bit {}
protocol IsSet {}
protocol IsClr {}
struct Set : Bit, IsSet {}
struct Clr : Bit, IsClr {}
protocol NandOp { typealias Result }
extension NandOp { typealias Result = Set }
struct Nand<B0: Bit, B1: Bit> : NandOp {}
extension Nand where B0: IsSet, B1: IsSet {
typealias Result = Clr
}
print(Nand<Clr, Clr>.Result.self == Set.self) // true
print(Nand<Clr, Set>.Result.self == Set.self) // true
print(Nand<Set, Clr>.Result.self == Set.self) // true
print(Nand<Set, Set>.Result.self == Clr.self) // true
@jepers
Copy link
Author

jepers commented Sep 28, 2015

Bummer:

struct Not<B0: Bit> {
    typealias Result = Nand<B0, B0>.Result // Error: Ambigous type name 'Result' in Nand<B0, B0>
}

:´ (

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment