Skip to content

Instantly share code, notes, and snippets.

@Inferis
Last active August 29, 2015 14:23
Show Gist options
  • Save Inferis/2714c636a6a443de4581 to your computer and use it in GitHub Desktop.
Save Inferis/2714c636a6a443de4581 to your computer and use it in GitHub Desktop.
Smashables and Values
//: Playground - noun: a place where people can play
protocol HeterogenousEquatable
{
func isEqual(other: HeterogenousEquatable) -> Bool
}
protocol Value: HeterogenousEquatable
{
}
protocol Smashable {
func valueBySmashing​OtherValue(value: Value) -> Value
}
struct Bar : Value
{
}
struct Baz : Value, Equatable
{
}
func ==(lhs: Baz, rhs: Baz) -> Bool
{
return true // exercise for the reader
}
extension Value where Self: Equatable
{
func isEqual(other: HeterogenousEquatable) -> Bool {
guard let selfOther = other as? Self else { return false }
return selfOther == self
}
}
struct Foo : Smashable
{
func valueBySmashing​OtherValue(value: Value) -> Value {
return Bar()
}
func valueBySmashing​OtherValue(value: Value) -> Bar {
return Bar()
}
func test() {
let f = Foo()
let z = Baz()
let b: Bar = f.valueBySmashing​OtherValue(v)
let ok = v == Bar()
let ok2 = z == Baz()
}
}
@Inferis
Copy link
Author

Inferis commented Jun 21, 2015

Updated, but this one crashes the compiler so I'm not sure it actually works.

@andymatuschak
Copy link

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