Last active
August 29, 2015 14:23
-
-
Save Inferis/2714c636a6a443de4581 to your computer and use it in GitHub Desktop.
Smashables and Values
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//: Playground - noun: a place where people can play | |
protocol HeterogenousEquatable | |
{ | |
func isEqual(other: HeterogenousEquatable) -> Bool | |
} | |
protocol Value: HeterogenousEquatable | |
{ | |
} | |
protocol Smashable { | |
func valueBySmashingOtherValue(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 valueBySmashingOtherValue(value: Value) -> Value { | |
return Bar() | |
} | |
func valueBySmashingOtherValue(value: Value) -> Bar { | |
return Bar() | |
} | |
func test() { | |
let f = Foo() | |
let z = Baz() | |
let b: Bar = f.valueBySmashingOtherValue(v) | |
let ok = v == Bar() | |
let ok2 = z == Baz() | |
} | |
} |
Yes, true. I have an updated version but it crashes the compiler. :)
Updated, but this one crashes the compiler so I'm not sure it actually works.
Hm, this one doesn't crash: https://gist.github.com/andymatuschak/a40c4c699c0abdd704ce
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hm, why make
isEqual
generic instead of using a protocol argument?